专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

oracle 统计合并后再行转列 oracle列数据合并在一个字段

ins518 2024-10-25 14:34:38 技术文章 7 ℃ 0 评论

原始需求:逐条统计的关联结果集;通过union all 合并结果集合 ,group by 分组,通过sum统计结果集;

增强需求: 需要将逐条统计的结果进行行转列的操作

select *

from (select s.jgmc, r.xsh, r.xb, sum(r.cws) as cws

from (select a.xsh, a.xb, a.cws

from yxxt_ssb a

where nvl(a.sfhq, '否') <> '是'

union all

select c.xsh, b.xb, c.fpme as cws

from yxxt_ssb b

left join yxxt_yxssmefbb c

on b.sslh = c.sslh

and b.fjh = c.fjh

where b.sfhq = '是') r

left join yxxt_code_jgb s

on r.xsh = s.jgdm

where r.xsh is not null

group by s.jgmc, r.xsh, r.xb) g

pivot(sum(g.cws)

for xb in('男' as boyrs, '女' as grilrs))

  • pivot(sum(g.cws) for xb in('男' as boyrs, '女' as grilrs)) 中的

①xb前面不能加别名!

②'男' as boyrs 中的 boyrs 不能添加单引号!

  • 返回的 * 也不能加别名;

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表