请教:Oracle 分组统计算法,分组 重复汇总

fineyun 2009-11-05 08:10:55
表结构和数据
字段 codeA codeB a b
数据 01 AAA 2 100
01 BBB 3 200
01 CCC 4 300
02 AAA 2 900
02 BBB 3 800
01 BBB 3 700

要求结果:
01 9 1300
02 5 1700

说明 汇总a时,codeB相同时,只计算一次,汇总B时,都计算


如何在一个语句中实现,请教各位高手了!
...全文
191 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灰狼W 2009-11-05
  • 打赏
  • 举报
回复
select codea,sum(distinct a),sum(b)
from tt
group by codea
liusong_china 2009-11-05
  • 打赏
  • 举报
回复
with tmp as
(select '01' codeA,'AAA' codeB,2 a,100 b from dual
union all select '01','BBB',3,200 from dual
union all select '01','CCC',4,300 from dual
union all select '02','AAA',2,900 from dual
union all select '02','BBB',3,800 from dual
union all select '01','BBB',3,700 from dual
)
select codeA,sum(a),sum(b) from
(select codeA,codeB,a,sum(b) b from tmp group by codeA,codeB,a)
group by codeA;

3,497

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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