ORAClE sql如何实现行转列?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ORAClE sql如何实现行转列?相关的知识,希望对你有一定的参考价值。

Oracle的表T记录如下图所示,要通过SQL将参数名变为列展示出来,请SQL高手帮忙!!!

要达到的效果:
参数名不是固定的,可能是其他名称。所以不能在SQL中写死

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

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.站名
参考技术A 如果“站名”、“条码”、“时间”都是一样的话,可以这么写:

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.站名
参考技术B select wm_concat(参数名) name from tablename group by 条码;

Hive 行转列 & 列转行

参考技术A hive中行转列和列转行比较常见,一般是用于中间数据的生产。

行转列一般采用IF进行判断,筛选出本列对应的数据进行统计。

如果我们直接

就会报错

那么,我们由此引入LATERAL VIEW函数

对一列进行炸裂,其他列保持不变

对多列进行炸裂,其他列保持不变

行和列转换的问题主要解决存储和计算过程中关联关系不对等的问题。

以上是关于ORAClE sql如何实现行转列?的主要内容,如果未能解决你的问题,请参考以下文章

SQLSERVER有张表,需要每晚自动实现行转列。

Hive 行转列 & 列转行

oracle行转列sql怎么写?

oracle行转列sql怎么写?

SQL语句实现行转列

sql语句行转列 怎么转啊