ORAClE sql如何实现行转列

ORAClE sql如何实现行转列,第1张

如果“站名”、“条码”、“时间”都是一样的话,可以这么写:

with

t_temp as (select row_number() over (partition by station_name order by param_name asc) id, t from t),

t_temp1 as (select from t_temp where id = 1),

t_temp2 as (select from t_temp where id = 2),

t_temp3 as (select from t_temp where id = 3)

select '站名' col1, '条码' col2, t_temp1参数名 col3, t_temp2参数名 col4, t_temp3参数名 col5, '时间' col6

from t_temp1, t_temp2, t_temp3

where t_temp1站名 = t_temp2站名

and t_temp2站名 = t_temp3站名

union all

select t_temp1站名, t_temp1条码, to_char(t_temp1数值), to_char(t_temp2数值), to_char(t_temp3数值), to_char(t_temp1时间)

from t_temp1, t_temp2, t_temp3

where t_temp1站名 = t_temp2站名

and t_temp2站名 = t_temp3站名

1、创建测试表,

create table test_fee(userid number, feeid number, fee number);

2、插入测试数据

insert into test_fee values(1,1001,80);

insert into test_fee values(1,1002,70);

insert into test_fee values(1,1003,90);

insert into test_fee values(1,1004,60);

insert into test_fee values(2,1001,99);

insert into test_fee values(2,1002,66);

insert into test_fee values(2,1001,55);

3、查询表中所有记录,select t, rowid from test_fee t,

4、编写sql,按userid汇总,根据不同的feeid,进行行转列汇总,

select userid,

     sum(case when feeid = 1001 then fee else 0 end) as fee_1001,

     sum(case when feeid = 1002 then fee else 0 end) as fee_1002,

     sum(case when feeid = 1003 then fee else 0 end) as fee_1003,

     sum(case when feeid = 1004 then fee else 0 end) as fee_1004

from test_fee t

group by userid

以上就是关于ORAClE sql如何实现行转列全部的内容,包括:ORAClE sql如何实现行转列、sql中一对多关系的查询结果的多行转换成一行多列、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10193234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存