如何转置Hive表

时间:2017-11-16 11:47:01

标签: hive pivot

有谁可以帮助我理解,如何转动Hive表。

我有一个像这样的Hive表:

ColumnA    ColumnB    Column C    Date        Price
A1          B1        C1          20171001    100
A1          B2        C2          20171001    50
A2          B2        C2          20171002    75
A3          B1        C2          20171002    10

我想像上面这样调整上表:

ColumnA    ColumnB    Column C    20171001    20171002    Total
A1         B1         C1          100                     100
A1         B2         C2          50                      50
A2         B2         C1                       75         75
A3         B1         C2                       10         10

1 个答案:

答案 0 :(得分:0)

试试这个。

select 
        ColumnA, 
        ColumnB, 
        ColumnC, 
        MAX ( CASE 
                WHEN Date_ = '20171001' 
                THEN Price 
        END ) price_20171001,
        MAX ( CASE 
                WHEN Date_ = '20171002' 
                THEN Price 
        END )      price_20171002 , 
        Max(Price) Total
FROM 
        Yourtable 
GROUP BY 
        ColumnA, 
        ColumnB, 
        ColumnC
ORDER BY 
        ColumnA;

注意:由于Date是无效列,我使用了Date_