【问题标题】:Not able to get the accurate count during aggregation based on condition在聚合期间无法根据条件获得准确的计数
【发布时间】:2019-03-13 08:07:15
【问题描述】:

我有一个名为 tbl1 的表。其中包含 3 列 empId、Designation 和 SalaryScale

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

我想获取具有特定指定为 DesCount 的人的总数,并且需要获取每个指定的salaryScale 计数,其中薪级 > 0 并将它们聚合到指定级别。

下面是我的查询和输出

选择 Designation,count(Designation) 作为 DesCount,count(salaryScale) as scaleCount from tbl1 wheresalaryScale>0 group by Designation;

Designation DesCount    scaleCount
Developer       1       1
ITA             1       1

但我期待的是

Designation DesCount    scaleCount
Developer       2       1
ITA             1       1

因为对于 Developer 指定,总数为 2。

我在查询中做错了吗?

【问题讨论】:

    标签: sql hive bigdata


    【解决方案1】:

    您可以在下面尝试使用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
    

    【讨论】:

      【解决方案2】:

      用例表达式

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-01
        • 2019-02-11
        • 2022-06-21
        • 2021-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        相关资源
        最近更新 更多