select 编号,数量,金额 from (
select 编号,数量,金额,类别,0 flag from A表
union all
select '总计:',null,sum(金额),类别,1 from A表 group by 类别
) tem order by 类别,flag

解决方案 »

  1.   

    如果你一次只要一组:
    select 编号,数量,金额 from A表 where 类别=1
    union all
    select '总计:',null,sum(金额) from A表 where 类别=1
      

  2.   

    如果你的编号是数字列:select cast(编号 as varchar(10)) 编号,数量,金额 from A表 where 类别=1
    union all
    select '总计:',null,sum(金额) from A表 where 类别=1
      

  3.   

    select 编号,数量,金额 from A表 where 类别=1
    union all
    select '总计:',null,sum(金额) from A表 where 类别=1orselect 编号,数量,金额 from A表 where 类别=2
    union all
    select '总计:',null,sum(金额) from A表 where 类别=2
      

  4.   

    恐怕不行啊!
    要用到GROUP BY的分组 而且是一个结果集出来啊!·
    大力的方法可以么?
    我是要按编号分组(根据不同条件)
      

  5.   

    select 编号,数量,金额 from A表 where 类别=1
    union all
    select '总计:',null,sum(金额) from A表 where 类别=1
      

  6.   

    SQL is not a good language for formatting, it is much easier to do it in your application or tryselect 编号,数量,金额, 类别 from your A表
    union
    select '总计',sum(数量) as 数量,sum(金额) as 金额, 类别 from your A表 group by 类别
      

  7.   

    select 编号,数量,金额 from 表 order by 类别,编号
    union
    select '总计:',null,sum(金额),类别,1 from A表 group by 类别
    ) tem order by 类别,flag
      

  8.   

    表记录如下
    编号  数量  金额  类别
    A01   10    20    1
    A02   10    30    1
    A03   5     15    2
    A03   7     21    2
    A01   15    30    1
    A02   5     15    1打印分组
    A01   25    50    1
    A02   15    45    1
    分组合计:   95 A03   12    36    2       
    请问可以做到么?
      

  9.   

    select 编号,数量,金额,类别 from (
    select cast(编号 as varchar(10)) as 编号,sum(数量) 数量,sum(金额) 金额,类别,0 flag from 表 group by 编号,类别
    union all
    select '总计:',null,sum(金额),类别,1 from 表 group by 类别
    ) tem order by 类别,flag
      

  10.   

    select 编号,数量,金额,类别 from (
    select 编号,sum(数量) 数量,sum(金额) 金额,类别,0 flag,类别 flag2 from A表 group by 类别,编号
    union all
    select '分组合计',null,sum(金额),null,1,类别 from A表 group by 类别
    ) tem order by flag2,flag