db2中如何实现 wmsys.wm_concat功能说的详细一点越详细越好,跪谢,贵谢
1个回答
展开全部
摘自博客:
之前弄过oracle的行转列了,现在研究下db2的行转列.
db2函数没有oracle那么强大,像oracle,几行代码能解决的问题换成db2的话,需要更多的代码了
下面粘出来,大家看下。要是发现问题给我留言哈
1.多行转一行
WITH rs as ( select name,row_number() over() RN from T_test),
RPL (RN, name) AS
(
select ROOT.RN,CAST(ROOT.name AS VARCHAR(2000)) from rs ROOT
UNION ALL
SELECT CHILD.RN, CHILD.name||','||PARENT.nameFROM RPL PARENT, rs CHILD WHERE PARENT.RN +1 = CHILD.RN
)
select MAX(name) namefrom RPL GROUP BY RN ORDER BY RN DESC
fetch first 1 rows only
2。行转列和oracle相同
===================================
with rs as(
select 'a' A, 2 B, 'www' C
from dual
union all
select 'b' A, 1 B, 'www' C
from dual
union all
select 'c' A, 3 B, 'sss' C
from dual
union all
select 'd' A, 4 B, 'ddd' C from dual)
/*
select rs.C,
case rs.A when 'a' then rs.B else 0 end ver_high,
case rs.A when 'b' then rs.B else 0 end v_high,
case rs.A when 'c' then rs.B else 0 end v_low,
case rs.A when 'd' then rs.B else 0 end ver_low
from rs
*/
select rs.C,
sum(case rs.A when 'a' then rs.B else 0 end) ver_high,
sum(case rs.A when 'b' then rs.B else 0 end) v_high,
sum(case rs.A when 'c' then rs.B else 0 end) v_low,
sum(case rs.A when 'd' then rs.B else 0 end) ver_low
from rs
group by C from rs
之前弄过oracle的行转列了,现在研究下db2的行转列.
db2函数没有oracle那么强大,像oracle,几行代码能解决的问题换成db2的话,需要更多的代码了
下面粘出来,大家看下。要是发现问题给我留言哈
1.多行转一行
WITH rs as ( select name,row_number() over() RN from T_test),
RPL (RN, name) AS
(
select ROOT.RN,CAST(ROOT.name AS VARCHAR(2000)) from rs ROOT
UNION ALL
SELECT CHILD.RN, CHILD.name||','||PARENT.nameFROM RPL PARENT, rs CHILD WHERE PARENT.RN +1 = CHILD.RN
)
select MAX(name) namefrom RPL GROUP BY RN ORDER BY RN DESC
fetch first 1 rows only
2。行转列和oracle相同
===================================
with rs as(
select 'a' A, 2 B, 'www' C
from dual
union all
select 'b' A, 1 B, 'www' C
from dual
union all
select 'c' A, 3 B, 'sss' C
from dual
union all
select 'd' A, 4 B, 'ddd' C from dual)
/*
select rs.C,
case rs.A when 'a' then rs.B else 0 end ver_high,
case rs.A when 'b' then rs.B else 0 end v_high,
case rs.A when 'c' then rs.B else 0 end v_low,
case rs.A when 'd' then rs.B else 0 end ver_low
from rs
*/
select rs.C,
sum(case rs.A when 'a' then rs.B else 0 end) ver_high,
sum(case rs.A when 'b' then rs.B else 0 end) v_high,
sum(case rs.A when 'c' then rs.B else 0 end) v_low,
sum(case rs.A when 'd' then rs.B else 0 end) ver_low
from rs
group by C from rs
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
威孚半导体技术
2024-08-19 广告
2024-08-19 广告
威孚(苏州)半导体技术有限公司是一家专注生产、研发、销售晶圆传输设备整机模块(EFEM/SORTER)及核心零部件的高科技半导体公司。公司核心团队均拥有多年半导体行业从业经验,其中技术团队成员博士、硕士学历占比80%以上,依托丰富的软件底层...
点击进入详情页
本回答由威孚半导体技术提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询