Grouping sets

  • Hi All,

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

    plz help out

  • 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

  • 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."

  • plz help out dis

  • 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))

  • Maybe consider vertical partitioning and recreate in a view?

  • can u write a query plz plz

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply