Hive 如何在蜂箱中转位
我需要一个更好的方法来处理以下在hive中的场景,因为hive不支持pivot 桌子 我需要转换成下面的结果 你可以试试这个Hive 如何在蜂箱中转位,hive,Hive,我需要一个更好的方法来处理以下在hive中的场景,因为hive不支持pivot 桌子 我需要转换成下面的结果 你可以试试这个 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' UNI
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
你可以试试这个
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
您可以使用brickhouse()中的
条件_emit
UDF来执行此操作
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(在传入的第一个数组中)的数组元素发出一条记录。请注意,这只对数据进行一次传递,而联合
将进行多次传递。您可以使用来自brickhouse()的条件_emit
UDF来执行此操作
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(在传入的第一个数组中)的数组元素发出一条记录。请注意,这只对数据进行一次传递,而联合将进行多次传递。请共享样本输出您所说的_col1是什么意思,_col2请共享样本输出您所说的_col1是什么意思,_col2如果您关心效率,此解决方案将是一场灾难。如果您关心效率,此解决方案将是一场灾难。