本帖最后由 skyzero110 于 2010-03-29 19:48:24 编辑

解决方案 »

  1.   

    create table #t(a int,b int,c int,d int,e int) 
    insert into #t values(1,2,3,4,5) 
    insert into #t values(1,2,3,4,6) 
    insert into #t values(1,2,3,4,7) 
    insert into #t values(1,2,3,4,8) 
    insert into #t values(1,3,3,4,5) 
    insert into #t values(1,3,3,4,6) 
    insert into #t values(1,3,3,4,8) 
    insert into #t values(1,3,3,4,7) insert into #t values(2,2,2,4,5) 
    insert into #t values(2,2,3,4,6) 
    insert into #t values(2,2,4,4,7) 
    insert into #t values(2,2,5,4,8) 
    insert into #t values(2,3,6,4,5) 
    insert into #t values(2,3,3,4,6) 
    insert into #t values(2,3,3,4,8) 
    insert into #t values(2,3,3,4,7)select 
      case when grouping(a)=1 then '合计' else cast(a as varchar) end a, b, sum(c) as c,sum(d) as d,sum(e) as e
    from 
      #t 
    group by 
      a,b 
    with rollup 
      having grouping(b)=0 or grouping(a)=1select 
      case when grouping(a)=1 then '合计' else cast(a as varchar) end a, b, c, sum(d) as d,sum(e) as e
    from 
      #t 
    group by 
      a,b,c 
    with rollup 
      having grouping(c)=0 or grouping(a)=1 select 
      case when grouping(a)=1 then '合计' else cast(a as varchar) end a, b, 
      case when grouping(c)=1 and grouping(b)=0 then '小计' else cast(c as varchar) end c, 
      sum(d) as d,sum(e) as e
    from 
      #t 
    group by 
      a,b,c 
    with rollup 
      having grouping(a)=1 or grouping(b)=0 select 
       case when grouping(a)=1 then '合计' else cast(a as varchar) end a, 
       case when grouping(b)=1 and grouping(a)=0 then '小计' else cast(b as varchar) end b, sum(c) as c, sum(d) as d,sum(e) as e from #t 
    group by 
       a,b,c 
    with rollup 
       having grouping(a)=1 or grouping(b)=1 or grouping(c)=0drop table #t/*a                              b           c           d           e           
    ------------------------------ ----------- ----------- ----------- ----------- 
    1                              2           12          16          26
    1                              3           12          16          26
    2                              2           14          16          26
    2                              3           15          16          26
    合计                             NULL        53          64          104(所影响的行数为 5 行)a                              b           c           d           e           
    ------------------------------ ----------- ----------- ----------- ----------- 
    1                              2           3           16          26
    1                              3           3           16          26
    2                              2           2           4           5
    2                              2           3           4           6
    2                              2           4           4           7
    2                              2           5           4           8
    2                              3           3           12          21
    2                              3           6           4           5
    合计                             NULL        NULL        64          104(所影响的行数为 9 行)a                              b           c                              d           e           
    ------------------------------ ----------- ------------------------------ ----------- ----------- 
    1                              2           3                              16          26
    1                              2           小计                             16          26
    1                              3           3                              16          26
    1                              3           小计                             16          26
    2                              2           2                              4           5
    2                              2           3                              4           6
    2                              2           4                              4           7
    2                              2           5                              4           8
    2                              2           小计                             16          26
    2                              3           3                              12          21
    2                              3           6                              4           5
    2                              3           小计                             16          26
    合计                             NULL        NULL                           64          104(所影响的行数为 13 行)a                              b                              c           d           e           
    ------------------------------ ------------------------------ ----------- ----------- ----------- 
    1                              2                              12          16          26
    1                              3                              12          16          26
    1                              小计                             24          32          52
    2                              2                              2           4           5
    2                              2                              3           4           6
    2                              2                              4           4           7
    2                              2                              5           4           8
    2                              3                              9           12          21
    2                              3                              6           4           5
    2                              小计                             29          32          52
    合计                             NULL                           53          64          104(所影响的行数为 11 行)*/
      

  2.   

    去看看 grouping的用法 是用
    grouping(col)=0或者1来控制的
      

  3.   

    DECLARE @t TABLE(Groups char(2),Item varchar(10),Color varchar(10),Quantity int)
    INSERT @t SELECT 'aa','Table','Blue', 124
    UNION ALL SELECT 'bb','Table','Red',  -23
    UNION ALL SELECT 'bb','Cup'  ,'Green',-23
    UNION ALL SELECT 'aa','Chair','Blue', 101
    UNION ALL SELECT 'aa','Chair','Red',  -90--汇总显示
    SELECT Groups=CASE 
            WHEN GROUPING(Color)=0 THEN Groups
            WHEN GROUPING(Groups)=1 THEN '总计'
            ELSE '' END,
        Item=CASE 
            WHEN GROUPING(Color)=0 THEN Item
            WHEN GROUPING(Item)=1 AND GROUPING(Groups)=0 THEN Groups+' 合计'
            ELSE '' END,
        Color=CASE 
            WHEN GROUPING(Color)=0 THEN Color
            WHEN GROUPING(Color)=1 AND GROUPING(Item)=0 THEN Item+' 小计'
            ELSE '' END,
        Quantity=SUM(Quantity)
    FROM @t
    GROUP BY Groups,Item,Color WITH ROLLUP
    /*--结果
    Groups Item       Color           Quantity    
    -------- ---------------- ---------------------- ----------- 
    aa     Chair      Blue            101
    aa     Chair      Red             -90
                     Chair 小计       11
    aa     Table      Blue            124
                     Table 小计       124
           aa 合计                    135
    bb     Cup        Green           -23
                      Cup 小计        -23
    bb     Table      Red             -23
                     Table 小计       -23
           bb 合计                    -46
    总计                              89
    --*/--------------------------
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/03/22/4014856.aspx
      

  4.   

    额. 又出现个问题.. 我select表是2484条记录  group by的时候是1561条数据
    但加个with rollup就7264行了.. 这样算求和不行啊..有重复数据
      

  5.   

    其实我有六列..  v1 v2 v3 v4 v5 v6
      

  6.   

    我想问下我group by数据是这样的白杆I 101-2B一头坏
    白杆I 101--2H副牌
    白杆I 101-4B7.2六角副牌
    白杆I 101--4B副牌
    白杆I 101--4H副牌 但是with rollup后变成了白杆I 101-2B一头坏
    白杆I 101-2B一头坏 NULL
    白杆I 101-2B一头坏 NULL NULL
    白杆I 101-2B一头坏 NULL NULL NULL
    白杆I 101-2B一头坏 NULL NULL NULL NULL
    白杆I 101--2H副牌
    白杆I 101--2H副牌 NULL
    白杆I 101--2H副牌 NULL NULL
    白杆I 101--2H副牌 NULL NULL NULL
    白杆I 101--2H副牌 NULL NULL NULL NULL