利用列转行函数减少表的访问

sql我只提供一需要改的sql,具体如下,写表已经经过替换:

select t.id, a as a,0 as LOC,col,col1,col2,col3
from tabne_name t
where a is not null
union all
select t.id,b as a,1 as LOC,col,col1,col2,col3
from tabne_name t 
where b is not null
union all
select t.id,c as a,2 as LOC,col,col1,col2,col3
from tabne_name t
where c is not null


改写后的sql如下

select id,new_vales,case when col_name='A' then 0
                    case when col_name='B' then 1
                    case when col_name='C' then 2 end as loc,col,col1,col2,col3
(select * from 
(select  id, a as a,b,c,col,col1,col2,col3 from table_name where a is not null or b is not null or c is not null)
unpivot(new_values for col_name in(a,b,c)))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值