计算两列中非NULL的行

时间:2014-01-23 18:15:42

标签: sql hive

我想在SQL中比较两个集合:

create table my_counts
as select count(*) as total, 
          count(c.cookie) as first,
          count(l.cookie) as second, 
          count(l.cookie and c.cookie) as common
from (select distinct s.cookie from ...) c
full outer join
     (select distinct s.cookie from ...) l
on c.cookie = l.cookie;
select * from my_counts;

但是,上述内容因此错误​​而失败:

FAILED: ClassCastException org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector cannot be cast to org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector

当然(假设上面的SQL语句做了我认为它应该做的事情),我应该total + common = first + second,所以common列并不是绝对必要的,但我仍然想知道是否有计算两列都是非NULL的行的方法。

1 个答案:

答案 0 :(得分:0)

AND - 是一个布尔运算符。似乎cookie是一个字符串,而不是布尔值。尝试将count(l.cookie and c.cookie)替换为:count(case when l.cookie is not null and c.cookie is not null then 1 else NULL end) as common