在基于条件的聚合期间无法获得准确的计数

bvuwiixz  于 2021-06-27  发布在  Hive
关注(0)|答案(2)|浏览(278)

我有一个名为tbl1的表。它包含3列empid、designation和salaryscale

empId   Designation     salaryScale
Arun    Developer          1
Kiran   Developer          0
Anu     ITA                2

我想得到的总人数有一个特定的指定为descount,需要得到每个指定的工资等级计数,其中工资等级>0,并在指定的水平上进行汇总。
下面是我的查询和输出
从tbl1中选择名称,计数(名称)作为描述计数,计数(工资等级)作为等级计数,其中工资等级>0按名称分组;

Designation DesCount    scaleCount
Developer       1       1
ITA             1       1

但我期望的是

Designation DesCount    scaleCount
Developer       2       1
ITA             1       1

因为对于开发人员指定,总计数是2。
我的问题有没有做错什么?

rjjhvcjd

rjjhvcjd1#

你可以在下面使用 case when expression 并移除 where condition 你的where条件是过滤掉然后它计数,所以你没有得到想要的结果

select Designation,count(Designation) as DesCount,
count(case when salaryScale>0 then 1 end) as scaleCount 
from tbl1  
group by Designation
guicsvcw

guicsvcw2#

用例表达式

select Designation,count(Designation) as DesCount,
sum(case when salaryScale>0 then 1 else 0 end) as scaleCount 
from table_name  
group by Designation

相关问题