oracle 行转列,列转行

1.行转列

1.未行转列的sql

select idx_key,idx_value,schoolcode from tb_ZS_coreidx_data where idx_key in (
'IDX_01_00_ZQRZZXSS',
'IDX_01_01_BSZRJSS',
'IDX_01_01_XSZRJSS') and schoolcode='10268'

结果
在这里插入图片描述
经过行转列后

with temp as(
select idx_key,idx_value,schoolcode from tb_ZS_coreidx_data where idx_key in (
'IDX_01_00_ZQRZZXSS',
'IDX_01_01_BSZRJSS',
'IDX_01_01_XSZRJSS') and schoolcode='10268'
)
select * fromselect schoolcode,idx_key,idx_value from temppivot (max(idx_value) for idx_key in ('IDX_01_00_ZQRZZXSS' as IDX_01_00_ZQRZZXSS,'IDX_01_01_BSZRJSS' as IDX_01_01_BSZRJSS,
'IDX_01_01_XSZRJSS' as IDX_01_01_XSZRJSS));

执行结果
在这里插入图片描述

2.列转行

1.未列转行sql

 select a.idx_key as idxkey1,b.idx_key as idxkey2,c.idx_key as idxkey3 from (
select IDX_KEY from tb_ZS_coreidx_data where schoolcode='10268' and idx_key in ('IDX_01_00_ZQRZZXSS' )) a ,
(select IDX_KEY from tb_ZS_coreidx_data where schoolcode='10268' and idx_key in ('IDX_01_01_BSZRJSS')) b ,
(select IDX_KEY from tb_ZS_coreidx_data where schoolcode='10268' and idx_key in ('IDX_01_01_XSZRJSS')) c

2.执行结果
在这里插入图片描述
经过列转行sql

with temp as(
 select a.idx_key as idxkey1,b.idx_key as idxkey2,c.idx_key as idxkey3 from (
select IDX_KEY from tb_ZS_coreidx_data where schoolcode='10268' and idx_key in ('IDX_01_00_ZQRZZXSS' )) a ,
(select IDX_KEY from tb_ZS_coreidx_data where schoolcode='10268' and idx_key in ('IDX_01_01_BSZRJSS')) b ,
(select IDX_KEY from tb_ZS_coreidx_data where schoolcode='10268' and idx_key in ('IDX_01_01_XSZRJSS')) c
)
select title,value from
temp
unpivot
(value for title in (idxkey1,idxkey2,idxkey3))t

执行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值