Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234

Generating SubTotals using GROUPING Expand / Collapse
Author
Message
Posted Tuesday, July 20, 2010 9:13 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:13 AM
Points: 35,996, Visits: 30,290
Hugo Kornelis (7/20/2010)
My final comment in this topic, and then I'll respect R.P.'s request.

Jeff Moden (7/19/2010)
Still, the order of run return is, in fact, documented in Books Online...

ROLLUP
Specifies that in addition to the usual rows provided by GROUP BY, summary rows are introduced into the result set. Groups are summarized in a hierarchical order, from the lowest level in the group to the highest. The group hierarchy is determined by the order in which the grouping columns are specified. Changing the order of the grouping columns can affect the number of rows produced in the result set.


This quote describes which possible summary rows are and are not introduced in the result set, not the order in which results are returned.

(I'm also tempted to point out the use of the term "result set" rather than "recordset" in this quote, but the terminology in BOL as a whole is so often wrong that I'd probably better not go there)

Jeff, or anyone else - if you want to continue this debate, then please start a new topic and send me a PM with the link, or include the link in this topic. Without a link, I'll probably never find the new topic.


It's important that people on this thread realize that WITH ROLLUP and WITH CUBE produce the correct sorted results as stated in BOL. How much plainer can you get than Groups are summarized in a hierarchical order, from the lowest level in the group to the highest.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #956048
Posted Friday, November 16, 2012 5:33 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, April 07, 2014 8:19 AM
Points: 48, Visits: 195
There are two things that I prefer to do in a different way:

The first is your use of ISNULL(FieldName, 'ZTotal') -- I prefer to use a case statement
CASE GROUPING(FieldName) WHEN 0 THEN FieldName ELSE 'Total' END -- this protects against a NULL value being mistaken for a total

The second is your order by using 'ZTotal' to make it go to the bottom -- I prefer to once again use the GROUPING() function
ORDER BY GROUPING(FieldName) -- which puts all the non-subtotals at the top since GROUPING() = 0 and the subtotals at the bottom where GROUPING() = 1
This also allows you to easily reverse the order and put the totals at the top by adding DESC -- and you don't have to worry about the dreaded 'Zygote' which sorts lower than 'ZTotal'

Another suggestion that I saw was using a Cube to get some the rollups to work in both directions -- you can always use a HAVING at the bottom to filter out any groups that you don't want, but you have to be very careful with the GROUP BY order or you'll lose more groupings
Examples:
HAVING GROUPING(FieldName) = 0 will not show any sub totals for that grouping
HAVING GROUPING(FieldWithID) = GROUPING(FieldWithValue) will not show any extra grouping for fields like StateID and StateValue
Post #1385623
Posted Friday, November 16, 2012 9:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 2:59 PM
Points: 6, Visits: 50
For a prettier output, prefix the product and scheme results with a space (' '+....)
Then you don't need to prefix the totals with a 'Z' as they will fall to the end anyway.
Post #1385746
Posted Friday, November 16, 2012 11:01 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, February 28, 2014 8:47 AM
Points: 72, Visits: 46
good article! and other comments from Hugo and gang. Just a comment, your query #2 is missing the column 'comment' on it.
Post #1385789
Posted Friday, November 16, 2012 11:54 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:46 AM
Points: 1,906, Visits: 10,945
Great article! My brain is finally starting to grasp what ROLLUP does.



Rob Schripsema
Accelitec, Inc
Post #1385813
Posted Monday, December 24, 2012 12:06 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, April 15, 2013 11:42 PM
Points: 424, Visits: 55
Nice article.
Post #1399955
« Prev Topic | Next Topic »

Add to briefcase «««1234

Permissions Expand / Collapse