Hive count(case when) / count(distinct)用法

1,839 阅读1分钟

count(表达式) :

如果表达式值为NULL则不加1,如果值都为NULL则结果为0

select 
count( case when c1 is not null then c1 else null end ) as num
from (
select 
null as c1
union ALL
select 
null as c1
)t1 -- 结果是0 ,结果不是NULL


select 
count( case when c1 is not null then c1 else null end ) as num
from (
select 
1 as c1
union ALL
select 
null as c1
)t1 -- 结果是1


select count(DISTINCT c1)
from 
(
select 2 as c1 
union ALL
select 1 as c1 
union ALL
select null as c1 
)t1 ; -- 2 ,不会包含NULL


end