如何在hive中忽略sum中的null值

时间:2017-01-18 14:24:22

标签: hive

我在hive中有一个表格,其值低于

ID  value
1
1

ID  value
1
1   2

在做和时我需要输出为

select id,sum(val) from table group by id;

首先要求输出

id sum
1

第二个表输出

id sum
1  2

2 个答案:

答案 0 :(得分:0)

如果您需要使用null sum过滤掉行,请使用having

select id, sum(value) from table group by id having sum(value) is not null;

答案 1 :(得分:0)

在数学默认2+0=2中,所以无论如何它都会起作用。不要担心这个hive将是默认会照顾这个。

hive> create table first (Id int,value int);
OK
Time taken: 3.895 seconds
hive> select * from first;
OK
1       2
1       NULL
hive> select id, sum(value) as sum from first group by id;
Total MapReduce CPU Time Spent: 4 seconds 610 msec
OK
1       2
Time taken: 83.483 seconds, Fetched: 1 row(s)