select 编号,数量,金额 from (
select 编号,数量,金额,类别,0 flag from A表
union all
select '总计:',null,sum(金额),类别,1 from A表 group by 类别
) tem order by 类别,flag
select 编号,数量,金额,类别,0 flag from A表
union all
select '总计:',null,sum(金额),类别,1 from A表 group by 类别
) tem order by 类别,flag
select 编号,数量,金额 from A表 where 类别=1
union all
select '总计:',null,sum(金额) from A表 where 类别=1
union all
select '总计:',null,sum(金额) 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
要用到GROUP BY的分组 而且是一个结果集出来啊!·
大力的方法可以么?
我是要按编号分组(根据不同条件)
union all
select '总计:',null,sum(金额) from A表 where 类别=1
union
select '总计',sum(数量) as 数量,sum(金额) as 金额, 类别 from your A表 group by 类别
union
select '总计:',null,sum(金额),类别,1 from A表 group by 类别
) tem order by 类别,flag
编号 数量 金额 类别
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
请问可以做到么?
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
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