繁体   English   中英

在Hive中转置许多列

[英]Transposing many columns in Hive

所有人,关于转置有很多问题,但是找不到最佳的解决方案。

我的数据是这种格式

> Customer| Status_A|Status_B|Status_C  
> 111|New | Null|New   
> 222|Old | Old |New   
> 333|Null| New |New 

我想要这种格式的结果

>Customer   Parameter   Status  
>111    A   New  
>111    B   Null  
>111    C   New  
>222    A   Old  
>222    B   Old  
>222    C   New  
>333    A   Null   
>333    B   New   
>333    C   New  

注意-1)我有50列这样的
2)我真的不需要结果数据中具有空值的行

寻找最有效的解决方案。 谢谢

最简单的方法是使用巨型union all

select customer, 'A' as parameter, status_a as status from t where status_a is not null union all
select customer, 'B' as parameter, status_b from t where status_b is not null union all
. . .

键入可能会很麻烦,因此建议您在电子表格中或使用其他SQL查询生成代码。

上面要求每列扫描一次表。 有时,更快的方法是使用cross join

select t.customer, p.parameter,
       (case p.parameter
            where 'a' then status_a
            where 'b' then status_b
            . . .
        end) as status
from t cross join
     (select 'a' as parameter union all
      select 'b' union all
      . . .
     ) p;

要消除NULL使其成为子查询,并where status is not NULL使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM