如何在Hive中转座

rvpgvaaj  于 2021-06-28  发布在  Hive
关注(0)|答案(2)|浏览(331)

我需要一个更好的方法来解决下面的场景,因为hive不支持pivot。
table

我需要转换成下面的结果。

2ekbmq32

2ekbmq321#

你可以试试这个

Select ID, Yes from (
SELECT ID, 'Col1' as 'Yes' where Col1  = 'yes'
UNION ALL
SELECT ID, 'Col2' as 'Yes' where Col2  = 'yes'
UNION ALL
SELECT ID, 'Col3' as 'Yes' where Col3  = 'yes'
UNION ALL
SELECT ID, 'Col4' as 'Yes' where Col4  = 'yes'
UNION ALL
SELECT ID, NULL as 'Yes' where Col1  != 'yes' and Col2  != 'yes' and Col3  != 'yes' and Col4  != 'yes'
) TempTable ORDER BY ID
toiithl6

toiithl62#

你可以用 conditional_emit 来自砖厂的自定义项(http://github.com/klout/brickhouse )做这个。

SELECT ID, ce.feature as yes
 FROM
 table
 LATERAL VIEW conditional_emit( 
        ARRAY( col1 = 'yes',
               col2 = 'yes',
               col3 = 'yes',
               col4 = 'yes',
               col1 != 'yes' and col2 != 'yes' and col3 != 'yes' and col4 != 'yes'),
        ARRAY( 'col1', 'col2', col3', 'col4', ' ' ) ) c as ce;
``` `conditional_emit` 是一个udtf,它将发出一个数组元素的记录,该数组元素为true(在传入的第一个数组中),并带有传入的第二个数组的相应字符串。请注意,这只会对数据进行一次传递,而 `UNION` 会多次传球。

相关问题