目录
1.简介
2.count,sum使用区别
3.实操
1.简介
在日常的工作中,总避免不了统计总数,Hive中常用的统计有两个函数
count
sum
可以在count,sum中里使用case when、distinct等处理,满足日常数据统计需求。
2.count sum使用区别
2.1 count
统计有效行(非null)
select count(*) from tmp.stu #统计所有行数
select count(id) from tmp.stu #统计id列不为null的行数
2.2 sum
将某列进行累加(如果某行的列的为null则忽略
select sum(id) from tmp.stu
3.实操
| id | sd | name |
|---|---|---|
| 1 | 111 | la |
| 2 | 222 | la |
| 3 | 333 | le |
| 4 | 444 | le |
| 5 | 555 | lz |
Hql
select
count(*) as s,
count(case when name == 'la' then 0 else 1 end) as s1,
count(case when name == 'la' then null else 1 end) as s2,
sum(case when name == 'la' then 0 else 1 end) as s3,
sum(case when name == 'la' then null else 1 end) as s4
from tmp.stu
结果
5 5 3 3 3
本文深入解析Hive中count与sum函数的区别及应用,通过实例演示如何在数据统计中结合case when、distinct等处理方式,满足复杂的数据分析需求。
3103

被折叠的 条评论
为什么被折叠?



