列换行

--方法1:通过union或union all转换
with tdata as (
select * from table( values('张三', 90.5, 88.5, 95.5, 100), ('李四', 90.5, 88.5, 95.5, 0) ) as t(fname, fmoney8, fmoney9, fmoney10, fmoney11)
)
select fname, fmoney8 as fmoney from tdata
union
select fname, fmoney9 as fmoney from tdata
union
select fname, fmoney10 as fmoney from tdata
union
select fname, fmoney11 as fmoney from tdata
order by fname, fmoney;

--方法2:通过合并、拆分转换
with n(num) as (
values (8) --自定义开始的值
union all
select num + 1 from n where num <= 10 --比最大长度小1
)
select * from (
--将字符串的金额转换成decimal
select fname, num as fnum, cast(fmoney as decimal(12,2) ) as fmoney from (
select fname, num,
length(rtrim(char(num-1))) as "数字的长度",
locate('_'||rtrim(char(num-1))||'_',fmoney)+2+length(rtrim(char(num-1))) as "开始位置", locate('_'||rtrim(char(num))||'_',fmoney) as "结束位置" ,
(locate('_'||rtrim(char(num))||'_',fmoney) - locate('_'||rtrim(char(num-1))||'_',fmoney) - 2 - length(rtrim(char(num-1))) ) as "长度",
--拆分每个金额
substr( fmoney, (locate('_'||rtrim(char(num-1))||'_',fmoney) + 2 + length(rtrim(char(num-1))) ), (locate('_'||rtrim(char(num))||'_',fmoney) - locate('_'||rtrim(char(num-1))||'_',fmoney) - 2 - length(rtrim(char(num-1))) ) ) as fmoney
from table( values('张三', 90.5, 88.5, 95.5, 100), ('李四', 90.5, 88.5, 95.5, 0) ) as t(fname, fmoney8, fmoney9, fmoney10, fmoney11),
--将多个金额合并
--转换成字符串的时候可能要去掉右边的空串:rtrim()
--decimal转换成char的时候左边多出几个0, 处理方法:char(cast(fmoney*100 as bigint)/100)
table( values('_7_' ||char(fmoney8) ||'_8_' ||char(fmoney9) ||'_9_' ||char(fmoney10)||'_10_'||rtrim(char(fmoney11))||'_11_') ) as t2(fmoney),
n
) as temp
) as temp where fmoney <> 0 order by fname, fmoney
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值