group by rollup 使用能指定列汇总小计吗?请教!

mfkpie 2013-12-15 12:10:50
编号 名称 规格 单位 数量 单价 金额
01 A1 1*3 台 10 2 20
02 A2 2*3 台 15 2 30
03 B1 3*4 条 6 3 18
04 B2 4*9 条 5 3 15

想要的结果为:

编号 名称 规格 单位 数量 单价 金额
01 A1 1*3 台 10 2 20
02 A2 2*3 台 15 2 30
03 B1 3*4 条 6 3 18
04 B2 4*9 条 5 3 15
合计: 36 83

能使用一条group with rollup处理吗?
...全文
337 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞啊子 2013-12-17
  • 打赏
  • 举报
回复
你需要的结果集 不适用"group with rollup" 还是 2楼的比较好。
xxfvba 2013-12-16
  • 打赏
  • 举报
回复
用union all 语句比较简单吧 select * from mf unoin all select '合计','','','',sum(数量),'',sum(金额) from mf
唐诗三百首 2013-12-15
  • 打赏
  • 举报
回复

create table mf(编号 varchar(5),名称 varchar(10),规格 varchar(10),单位 varchar(5),数量 int,单价 int,金额 int)

insert into mf 
 select '01','A1','1*3','台',10,2,20 union all
 select '02','A2','2*3','台',15,2,30 union all
 select '03','B1','3*4','条',6,3,18 union all
 select '04','B2','4*9','条',5,3,15


select case grouping(编号) when 1 then '合计:' else 编号 end '编号',
       case grouping(编号) when 1 then '' else 名称 end '名称',
       case grouping(编号) when 1 then '' else 规格 end '规格',
       case grouping(编号) when 1 then '' else 单位 end '单位',
       case grouping(编号) when 1 then sum(数量) else max(数量) end '数量',
       case grouping(编号) when 1 then '' else rtrim(单价) end '单价',
       case grouping(编号) when 1 then sum(金额) else max(金额) end '金额'
 from mf
 group by 编号,名称,规格,单位,单价
 with rollup
 having grouping(编号)=1 or grouping(单价)=0

/*
编号    名称         规格         单位    数量          单价           金额
----- ---------- ---------- ----- ----------- ------------ -----------
01    A1         1*3        台     10          2            20
02    A2         2*3        台     15          2            30
03    B1         3*4        条     6           3            18
04    B2         4*9        条     5           3            15
合计:                               36                       83

(5 row(s) affected)
*/

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧