Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8830991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:12:52+00:00 2026-06-14T08:12:52+00:00

I need to query some data in the below format in SQL Server: Id

  • 0

I need to query some data in the below format in SQL Server:

  • Id Group Price
  • 1 A 10
  • 2 A 20
  • Sum 30

  • 1 B 6

  • 2 B 4
  • Sum 10

  • 1 C 100

  • 2 C 200
  • Sum 300

I was thinking to do it in the follwoing steps:

  1. Query one group
  2. In other query do sum
  3. Use Union operator to combine this result set
  4. Do step 1-3 for all groups and finally return all sub sets of data using union.

Is there a better way to do this ? May be using some out of box feature ? Please advise.

Edit:

As per suggestions and code sample I tried this code:

Select 
Case 
when id is null then 'SUM' 
else CAST(id as Varchar(10)) end as ID,

Case when [group] is null then 'ALL' else CAST([group] as Varchar(50)) end as [group]
,Price from
(
SELECT Id,  [Group],BGAApplicationID,Section, SUM(PrimaryTotalArea) AS price   
FROM vwFacilityDetails
where bgaapplicationid=1102
GROUP BY Id,  [Group],BGAApplicationID,Section  WITH ROLLUP
) a

And Even this code as well:

Select Id,  [Group],BGAApplicationID,Section, SUM(PrimaryTotalArea) AS price           
From   vwFacilityDetails
Where  Not ([group] Is Null And id Is Null And BGAApplicationId is null and section is null) and BGAApplicationId=1102
Group By Id,  [Group],BGAApplicationID,Section 
    With Rollup

In results it groups up the data but for every record it shows it 3 times (in both above codes) like:

  • 2879 Existing Facilities Whole School 25.00
  • 2879 Existing Facilities Whole School 25.00
  • 2879 Existing Facilities Whole School 25.00
  • 2879 ALL 25.00

I guess there is some issue in my query, please guide me here as well.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:12:53+00:00Added an answer on June 14, 2026 at 8:12 am

    SQL Server introduced GROUPING SETS which is what you should be looking to use.

    SQL Fiddle

    MS SQL Server 2008 Schema Setup:

    Create Table vwFacilityDetails (
      id int not null,
      [group] char(1) not null,
      PrimaryTotalArea int not null,
      Section int,
      bgaapplicationid int
    );
    
    Insert Into vwFacilityDetails (id, [group], Section,bgaapplicationid,PrimaryTotalArea) values
      (1, 'A', 1,1102,2),
      (1, 'A', 1,1102,1),
      (1, 'A', 1,1102,7),
      (2, 'A', 1,1102,20),
      (1, 'B', 1,1102,6),
      (2, 'B', 1,1102,4),
      (1, 'C', 1,1102,100),
      (2, 'C', 1,1102,200);
    

    Query 1:

    SELECT CASE WHEN Id is null then 'SUM'
                ELSE Right(Id,10) end Id,
           [Group],BGAApplicationID,Section,
           SUM(PrimaryTotalArea) price   
    FROM vwFacilityDetails
    where bgaapplicationid=1102
    GROUP BY GROUPING SETS (
      (Id,[Group],BGAApplicationID,Section),
      ([Group])
    )
    ORDER BY [GROUP],
             ID;
    

    Results:

    |  ID | GROUP | BGAAPPLICATIONID | SECTION | PRICE |
    ----------------------------------------------------
    |   1 |     A |             1102 |       1 |    10 |
    |   2 |     A |             1102 |       1 |    20 |
    | SUM |     A |           (null) |  (null) |    30 |
    |   1 |     B |             1102 |       1 |     6 |
    |   2 |     B |             1102 |       1 |     4 |
    | SUM |     B |           (null) |  (null) |    10 |
    |   1 |     C |             1102 |       1 |   100 |
    |   2 |     C |             1102 |       1 |   200 |
    | SUM |     C |           (null) |  (null) |   300 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to SQL Server 2008, and I need some help in query
I need some help with SQL Query. I am trying to select all records
I need to clean out some old data from my database tables. Below shows
I have some data in JSON format in a text file as below. I
I am having some trouble building the query I need to return the AccountID
Need some help with a query.. I have three tables. Source id name 1
I need some help a query. For each movie in my database that has
I need some help putting together this query in Django. I've simplified the example
I need some help with a LINQ query in VB.Net, please. I have this
I need some help adding conditions to a linq query. Take this small example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.