Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 grouping

Author  Topic 

kwacz23
Starting Member

44 Posts

Posted - 2011-10-23 : 13:20:19
Hey,
Please help me with grouping. how does it work in this example?
what is the difference between grouping or without grouping in this example?

SELECT orderid, GROUPING (orderid)
,productid, GROUPING (productid)
,SUM(quantity) AS total_quantity
FROM [order details]
WHERE orderid < 10250
GROUP BY orderid, productid
WITH CUBE
ORDER BY orderid, productid

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-23 : 13:44:48
the rows for which grouping gets 1 indicates they're summary values for group based on orderid/productid values and for them there will be NULL value in fields orderid/productid. any other NULL valued record for them with grouping value as 0 means they're not part of aggregate but rather they denote the value for group of records with NULL values for orderid/productid
You will notice difference only if fields orderid/productid are Nullable.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -