SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Grouping sets


Grouping sets

Author
Message
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
Hi All,


how to use the groupingsets to a query having more than 32 columns


plz help out
SQL_Enthusiast
SQL_Enthusiast
SSC Eights!
SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)

Group: General Forum Members
Points: 890 Visits: 422
Hello cooljagadeesh,

Can you provide us with a little more detail please? What exactly are you trying to group?
If you're just looking for basic information on grouping, there's a nice article by Craig Freedman.

Link: http://blogs.msdn.com/b/craigfr/archive/2007/10/11/grouping-sets-in-sql-server-2008.aspx
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
please take below example


declare @tbl table (Col1 int,Col2 int,Col3 int,Col4 int,Col5 int,Col6 int,Col7 int,
Col8 int,Col9 int,Col10 int,Col11 int,Col12 int,Col13 int,Col14 int,Col15 int,Col16 int,
Col17 int,Col18 int,Col19 int,Col20 int,Col21 int,Col22 int,Col23 int,Col24 int,Col25 int,
Col26 int,Col27 int,Col28 int,Col29 int,Col30 int,Col31 int,Col32 int,Col33 int,col34 int)


insert @tbl
select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
union
select 10,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
union
select 20,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
union
select 30,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
union
select 40,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34





select SUM(Col1) Total,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,
Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25,Col26,Col27,Col28,Col29,Col30 from @tbl
group by
grouping sets ((),(Col1,Col2),(Col1,Col2,Col3),(Col1,Col2,Col3,Col4),(Col1,Col2,Col3,Col4,COL5),
(Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,
Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25,Col26,Col27,Col28,Col29,Col30,col31,col32,col33))

plz check

after executing above query its giving error

plz lookinto my issue

"Msg 10706, Level 16, State 1, Line 27
Too many expressions are specified in the GROUP BY clause. The maximum number is 32 when grouping sets are supplied."
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
plz help out dis
SQL_Enthusiast
SQL_Enthusiast
SSC Eights!
SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)SSC Eights! (890 reputation)

Group: General Forum Members
Points: 890 Visits: 422
This is probably the error message you are receiving, correct?

Msg 10706, Level 16, State 1, Line 27
Too many expressions are specified in the GROUP BY clause. The maximum number is 32 when grouping sets are supplied.


For a GROUP BY clause that uses ROLLUP, CUBE, or GROUPING SETS, the maximum number of expressions is 32. You will have to remove one of the grouping sets to get the query to finish successfully. Here is a link on GROUPING. http://msdn.microsoft.com/en-us/library/ms177673.aspx

If you drop column 33, the query would run.


declare @tbl table (Col1 int,Col2 int,Col3 int,Col4 int,Col5 int,Col6 int,Col7 int,
Col8 int,Col9 int,Col10 int,Col11 int,Col12 int,Col13 int,Col14 int,Col15 int,Col16 int,
Col17 int,Col18 int,Col19 int,Col20 int,Col21 int,Col22 int,Col23 int,Col24 int,Col25 int,
Col26 int,Col27 int,Col28 int,Col29 int,Col30 int,Col31 int,Col32 int,Col33 int,col34 int)


insert @tbl
select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
union
select 10,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
union
select 20,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
union
select 30,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
union
select 40,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34

--SELECT * FROM @tbl AS t



select SUM(Col1) Total,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,
Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25,Col26,Col27,Col28,Col29,Col30 from @tbl
group by
grouping sets ( (),(Col1,Col2),(Col1,Col2,Col3),(Col1,Col2,Col3,Col4),(Col1,Col2,Col3,Col4,COL5),
(Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,
Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25,Col26,Col27,Col28,Col29,Col30,col31,col32))


Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2127 Visits: 1119
Maybe consider vertical partitioning and recreate in a view?
cooljagadeesh
cooljagadeesh
SSC-Enthusiastic
SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)SSC-Enthusiastic (150 reputation)

Group: General Forum Members
Points: 150 Visits: 223
can u write a query plz plz
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search