|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, September 27, 2012 4:24 AM
Points: 253,
Visits: 78
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Sunday, May 12, 2013 4:01 AM
Points: 406,
Visits: 1,364
|
|
When you want to identify the "scheme totals"-rows in your result set you need to add another GROUPING() column, this time specifying the Scheme_Name column for its parameter.
SELECT ISNULL(Product_Name,'ZProductTotal') as Product_name, ISNULL(Scheme_Name,'ZTotal') as Scheme_Name, SUM(Invest_Amount) AS [Invest_Amount], SUM(broker_commission) AS [Broker_Commission], GROUPING(Scheme_Name) AS [IsSchemeTotal], GROUPING(Product_Name) AS [IsGrandTotal] FROM @GroupTest GROUP BY Product_Name,Scheme_Name WITH ROLLUP ORDER BY Product_Name,Scheme_Name, IsSchemeTotal, IsGrandTotal
An excerpt of the output is now: Product_name Scheme_Name Invest_Amount Broker_Commission IsSchemeTotal IsGrandTotal ... Product9 Scheme5 480107.25 39160.80 0 0 Product9 ZTotal 5304042.00 432633.60 1 0 ZProductTotal ZTotal 24747623.55 2018583.84 1 1
The first row in the excerpt shows the figures for Product9 in Scheme5, both grouping columns are 0, indicating this row is not a (sub-)totals row. On the 2nd row the IsSchemeTotal column returns 1, indicating this rows holds the sub-total for all schemes for Product9. On the 3rd row you see both IsSchemeTotal and IsGrandTotal set to 1, indicating this row holds the figures for both all schemes and all products, i.e. the grand total.
As an extra note: if you change the order by clause to ORDER BY IsGrandTotal, Product_Name, IsSchemeTotal, Scheme_Name , you'l always get the 'normal' rows before the subtotals before the grand totals.
Posting Data Etiquette - Jeff Moden Posting Performance Based Questions - Gail Shaw Hidden RBAR - Jeff Moden Cross Tabs and Pivots - Jeff Moden Catch-all queries - Gail Shaw
If you don't have time to do it right, when will you have time to do it over?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 5,243,
Visits: 7,055
|
|
Good article, andd good addition by R.P.Rozema.
Remember that WITH ROLLUP is deprecated as of SQL Server 2010. It has been replaced with GROUPING_SETS.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 8:09 AM
Points: 310,
Visits: 150
|
|
With Rollup can be replaced with Rollup
SELECT Product_Name, Scheme_Name, Invest_Amount, broker_commission, -1 AS IsSchemeTotal, -1 as IsGrandTotal FROM @GroupTest Union ALL select isnull(Product_Name, 'ZProductTotal'), isnull(Scheme_Name,'ZTotal'), sum(Invest_Amount), SUM(Broker_Commission), GROUPING(Scheme_Name) AS IsSchemeTotal, GROUPING(Product_Name) AS IsGrandTotal from @grouptest group by rollup (Product_Name,Scheme_Name) ORDER BY Product_Name, Scheme_Name, IsSchemeTotal, IsGrandTotal
To give the same results
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 4:52 PM
Points: 1,379,
Visits: 2,626
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, July 22, 2010 8:59 AM
Points: 110,
Visits: 952
|
|
| Rather than union all of 4 separate selects against the same base table, would 4 CTE based on each other be better performing? On SQL2000 (no CTE) we tend to run a lot into temp tables. In this scenario it usually makes sense to aggregate on the aggregated table so each select processes less source rows. ( million detail rows, 100k daily, 20k weekly, 5k monthly, etc ) Why process the million detail rows 4 times?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 10, 2012 12:19 PM
Points: 103,
Visits: 301
|
|
| Here is another good definition of using this style of groups
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Friday, March 01, 2013 11:16 AM
Points: 67,
Visits: 188
|
|
How would one go about grouping by dates, i.e. month, year?
So a query from 12/30/2009 thru 1/2/2010 would have something similar to this.
Meter Day Month Year Barrels ``````````````````````````````````````` Meter1 12/30/09 Dec 2009 5 Meter1 12/31/09 Dec 2009 10 Meter1 DecTotal 2009 15 Meter1 2009Total 15 Meter2 1/1/10 Jan 2010 8 Meter2 1/2/10 Jan 2010 12 Meter2 JanTotal 2010 20 Meter2 2010Total 20
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
Wow. Did I mess up on this one. Let me correct myself before someone else has to.
The answer to Sunil's question about why only the grand total line got a 1 indicator is quite simple. The GROUPING column was defined to indicate rows added by ROLLUP for a total of all Product_Name subtotals. To get an indcator for those subtotals, we'd have to add another column, GROUPING(Scheme_Name), which will indicate rows added by ROLLUP for totals of all Scheme_Names in a Product_Name.
SELECT Product_Name, Scheme_Name, Invest_Amount, broker_commission, -1 AS GrandTotal, -1 as ProductTotal --<=== Added this FROM @GroupTest UNION ALL SELECT ISNULL(Product_Name,'ZProductTotal'), ISNULL(Scheme_Name, 'ZTotal'), SUM(Invest_Amount) AS 'Invest_Amount', SUM(broker_commission) AS 'Broker_Commission', GROUPING(Product_Name)'GrandTotal', GROUPING(Scheme_Name)'ProductTotal' --<==== added this FROM @GroupTest GROUP BY Product_Name, Scheme_Name WITH ROLLUP ORDER BY Product_Name, Scheme_Name, GrandTotal From the article:
Grouping separates the NULL values that are returned by ROLL UP from normal null values. In the above example (Method 2) the Totals and the Grand Total rows are actually generated by the ROLL UP operation. ROLL UP adds the NULL value for the summary row. Grouping separates this NULL by indicating 1 against the row. Hence in the above output (Method 2) the Grand Total summary row is indicated by a 1.
Here one question comes in the mind that why only one row is indicated by 1? The Scheme_Total and Product_Total is also aggregated but it is not indicated by 1. I have not yet received any satisfactory answer for this. I will post the answer here once I get it.
Let's start over with what GROUPING does. MSDN tells us that GROUPING is "... an aggregate function that causes an additional column to be output with a value of 1 when the row is added by either the CUBE or ROLLUP operator, or 0 when the row is not the result of CUBE or ROLLUP."
This is wrong. Let me strike it out.....
Now we can answer the question as to why only one row gets the GROUPING indcator of 1 by recognizing that it is the only row that's inserted into the result set by the ROLLUP. The Product_Total amounts are indeed aggregated, but that's done by the GROUP BY, not by the ROLLUP.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|