针对特定方案的Hive查询

时间:2015-03-27 08:05:31

标签: hadoop hive hiveql

我有一个我想编写Hive查询的场景。场景如下:

数据如下:

enter image description here

现在,我期待的输出是:

enter image description here

要忽略的空值/空值。

基本上,我想要列col1,col2,col3和col4中的值之和并对值进行分组。 “id”列在此处无关紧要,但仅供参考。

为此方案编写HiveQL查询的任何帮助都会有所帮助。

感谢。

2 个答案:

答案 0 :(得分:1)

select val, count(1)
from test_table lateral view explode(array(col1,col2,col3,col4)) t as val
where val is not null
group by val

解释

test_table是您的表名。
如果列数多于4,请将它们放在array函数中。

首先,我将所有列收集到一个数组中。然后使用lateral view explode(array)将数组项目放在一列中。在此列的最后一组中进行计数。

答案 1 :(得分:0)

在HIVE中实现这一目标的另一种方法

select e.single_col,count(e.single_col) from (
select stack(4,col1,col2,col3,col4) as single_col from challenge ) e
where e.single_col != ''
group by e.single_col;

挑战是这里使用的表格。 数据以逗号

分隔的文本格式加载到表中
1,A,B,,D
2,B,C,D,
3,,D,C,
4,,A,A,