如何在HIVE中由长到宽的结构?

问题描述 投票:0回答:1

我有一个类似[data in long format]的配置单元数据>

我想对其进行重组以使其看起来像data in wide format

我的外观如下my latest attempt

这是我使用的代码。我想摆脱所有NULL,并将所有月份合并为一年以下的一行。

select carrier, year, month,

max(case when month =1 then quantity end) as jan,
max(case when month =2 then quantity end) as feb,
max(case when month =3 then quantity end) as mar,
max(case when month =4 then quantity end) as apr,
max(case when month =5 then quantity end) as may,
max(case when month =6 then quantity end) as jun,
max(case when month =7 then quantity end) as jul,
max(case when month =8 then quantity end) as aug,
max(case when month =9 then quantity end) as sep,
max(case when month =10 then quantity end) as oct,
max(case when month =11 then quantity end) as nov,
max(case when month =12 then quantity end) as dec

from (select final_month.*, row_number() over 
(partition by carrier, year order by carrier, year) from final_month) 
final_month group by carrier, year, month;

这里是我一直在使用的link link的所有引用

谢谢!

我有一个蜂巢数据,看起来像是长格式的数据,我想将其重构为看起来像是宽格式的数据,这是我最近的尝试,这是我的代码...

hadoop hive hiveql reshape
1个回答
0
投票

从选择和分组依据中删除month。而且我相信您想要的是SUM,而不是max(),但是您当然更了解数据:

© www.soinside.com 2019 - 2024. All rights reserved.