Hadoop 如何在蜂巢中从长到宽的结构?

Hadoop 如何在蜂巢中从长到宽的结构?,hadoop,hive,hiveql,reshape,Hadoop,Hive,Hiveql,Reshape,我有一个蜂巢数据,看起来像这样 我想把它改造成这样 我所拥有的看起来像这样 这是我用过的代码。我想去掉所有的空值,把所有的月份合并到一年下的一行中 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 m

我有一个蜂巢数据,看起来像这样

我想把它改造成这样

我所拥有的看起来像这样

这是我用过的代码。我想去掉所有的空值,把所有的月份合并到一年下的一行中

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;
这是我使用过的所有参考资料


谢谢大家!

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

select carrier, year, 
sum(case when month =1 then quantity end) as jan,
sum(case when month =2 then quantity end) as feb,
sum(case when month =3 then quantity end) as mar,
sum(case when month =4 then quantity end) as apr,
sum(case when month =5 then quantity end) as may,
sum(case when month =6 then quantity end) as jun,
sum(case when month =7 then quantity end) as jul,
sum(case when month =8 then quantity end) as aug,
sum(case when month =9 then quantity end) as sep,
sum(case when month =10 then quantity end) as oct,
sum(case when month =11 then quantity end) as nov,
sum(case when month =12 then quantity end) as dec
from ...
group by carrier, year

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

select carrier, year, 
sum(case when month =1 then quantity end) as jan,
sum(case when month =2 then quantity end) as feb,
sum(case when month =3 then quantity end) as mar,
sum(case when month =4 then quantity end) as apr,
sum(case when month =5 then quantity end) as may,
sum(case when month =6 then quantity end) as jun,
sum(case when month =7 then quantity end) as jul,
sum(case when month =8 then quantity end) as aug,
sum(case when month =9 then quantity end) as sep,
sum(case when month =10 then quantity end) as oct,
sum(case when month =11 then quantity end) as nov,
sum(case when month =12 then quantity end) as dec
from ...
group by carrier, year

您也可以使用
group\u map
可以告诉我group\u map使用的示例吗?谢谢。我的脚本是基于这一点编写的,但我无法真正计算出组图,因此如果有逐步的解释就太好了。你也可以使用
group\u map
可以告诉我组图使用的示例吗?谢谢。我的剧本是基于这个,但我不能真正画出组图,所以如果有一步一步的解释就好了。