我用select * from table group by 字段1,字段2 with rollup having grouping(字段1)+grouping(字段2)……得出按字段1,字段2分类汇总,汇总的列都是在该类的最后一行出现,能否在该类的第一行也出现一行这类的标识。

解决方案 »

  1.   

    打开
    http://hi.csdn.net/cp.php?ac=upload
    然后上传你的图片,再打图片地址帖过来。
      

  2.   

    select * from tb group by ..
    union all
    select * from table group by 字段1,字段2 with rollup having grouping(字段1)+grouping(字段2)……
      

  3.   

    是不是應該上下對調一下union
      

  4.   


    create table tb(id int,name1 varchar(10),name2 varchar(10))
    insert tb
    select 1 ,'a' ,'a' union all
    select 2 ,'c' ,'a'union all
    select 3 ,'b','a'union all
    select 4 ,'a' ,'a' 
    select  isnull(name1,'总计')name1,
            isnull(name2,case when name1 is not null then '小计' end )name2,
            sum(id) as [合计] from tb group by name1,name2 with rollup
    order by case when name1 is null then 0 else 1 end,
             name1,
             case when name2 is null then 0 else 1 end
    drop table tb/*
    name1      name2      合计          
    ---------- ---------- ----------- 
    总计         NULL       10
    a          小计         5
    a          a          5
    b          小计         3
    b          a          3
    c          小计         2
    c          a          2
    */