| Author |
Topic |
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-06-22 : 09:25:16
|
im very new to sql programming. I wonder at which situation we use codes something like belowcase grouping(field) when.......... I come across many such examples from Google when i try to learn subtotal, crosstab. Also I have one in one of my threads.http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=162981I would like to know wen we use "grouping" particularly. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-06-22 : 11:36:03
|
From Books Online:quote: GROUPING is used to distinguish the null values that are returned by ROLLUP, CUBE or GROUPING SETS from standard null values. The NULL returned as the result of a ROLLUP, CUBE or GROUPING SETS operation is a special use of NULL. This acts as a column placeholder in the result set and means all.
Basicallt it allows you to know which row is a "Grouping" row, so you can deal with appropriatly if you want. for example you could replace the NULL with a string like Total or something. For example:DECLARE @T TABLE (ColA INT, ColB INT)INSERT @T VALUES(1, 1),(1, 2),(1, 3),(2, 2),(3, 4),(3, 5),(5, 5),(5, 8),(6, 6),(7, 3),(7, 8)SELECT ColA, SUM(ColB)FROM @TGROUP BY ColAWITH ROLLUPSELECT CASE WHEN GROUPING(ColA) = 1 THEN 'Total' ELSE CAST(ColA AS VARCHAR(20)) END AS ColA, SUM(ColB), GROUPING(ColA)FROM @TGROUP BY ColAWITH ROLLUP |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-22 : 22:33:13
|
| mostly used in cases where you return data grouped on multiple column combinations and would like to apply some conditional logic and determine display options/retrieve value of another field based on it------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-06-23 : 06:57:55
|
| Thanks for replyI'm still confused a bit. Maybe I can understand once I see the output and difference myself.For that, i tried to copy and paste entire @Lamprey code in my sql server management studio 2005 under "new query" window. When i execute, it get error "incorrect syntax...". Am I pasting it in wrong area? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-23 : 13:34:11
|
quote: Originally posted by learning_grsql Thanks for replyI'm still confused a bit. Maybe I can understand once I see the output and difference myself.For that, i tried to copy and paste entire @Lamprey code in my sql server management studio 2005 under "new query" window. When i execute, it get error "incorrect syntax...". Am I pasting it in wrong area?
are you using sql 2008 or above? Lampreys insert code works only on sql 2008 or above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-06-23 : 15:32:05
|
| Mine is sql 2005 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-23 : 18:37:13
|
then modify it likeDECLARE @T TABLE (ColA INT, ColB INT)INSERT @T SELECT 1, 1 UNION ALLSELECT 1, 2 UNION ALLSELECT 1, 3 UNION ALLSELECT 2, 2 UNION ALLSELECT 3, 4 UNION ALLSELECT 3, 5 UNION ALLSELECT 5, 5 UNION ALLSELECT 5, 8 UNION ALLSELECT 6, 6 UNION ALLSELECT 7, 3 UNION ALLSELECT 7, 8SELECT ColA, SUM(ColB)FROM @TGROUP BY ColAWITH ROLLUPSELECT CASE WHEN GROUPING(ColA) = 1 THEN 'Total' ELSE CAST(ColA AS VARCHAR(20)) END AS ColA, SUM(ColB), GROUPING(ColA)FROM @TGROUP BY ColAWITH ROLLUP ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-06-24 : 10:55:26
|
| Great! Thank you. It's working now.But I have a question on this. What exactly the statement below does in this code?else cast((col A as Varchar (20)) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-24 : 12:59:58
|
| its making datatype of ColA as varchar. Reason is CASE WHEN can return only singe datatype value. So since you want word Total to appear for total row you've to convert the other values of ColA also to varchar (its INT in table)which is why you've apply CAST------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
learning_grsql
Posting Yak Master
230 Posts |
Posted - 2012-06-25 : 11:22:14
|
| Thank you...I have understood it completely now. |
 |
|
|
|