简体   繁体   English

hive 查询中的多个计数未给出预期结果

[英]Multiple counts in hive query not giving expected results

I have data as follows.我有如下数据。

在此处输入图像描述

I am trying to get the following results with the query what I have developed.我试图通过我开发的查询获得以下结果。

select date,name,act_cd,type_cd, type 
count(distinct CASE WHEN act_cd in (1) THEN key_id ELSE 0 END) as count-of-AB,
count(distinct CASE WHEN act_cd in (2) THEN key_id ELSE 0 END) as count-of-CD
FROM table 
where act_cd in (1,2)
and type in (MR,LP)
group by date,name,act_cd,type_cd,type

Expected Results预期成绩

在此处输入图像描述

But its giving following results.但它给出了以下结果。 Not sure what is the wrong with the query and could soneone let me know how can we get expected results.不确定查询有什么问题,可以让我知道如何获得预期的结果。

Actual Results实际结果

在此处输入图像描述

Really appreciate your help.非常感谢您的帮助。

Thanks,Babu谢谢,巴布

Remove the ELSE :删除ELSE

select date, name, type_cd, type 
       count(distinct CASE WHEN act_cd in (1) THEN key_id END) as count_of_AB,
       count(distinct CASE WHEN act_cd in (2) THEN key_id END) as count_of_CD
from table 
where act_cd in (1, 2) and
      type in ('MR','LP')
group by date, name, type_cd, type;

The issue with your code is that the 0 counts as a value for count(distinct) .您的代码的问题是0算作count(distinct)的值。

In addition, it is not appropriate to have act_cd in the group by .此外,在group by中有act_cd是不合适的。

If key_id is guaranteed to be unique (at least within a group), then don't use distinct .如果key_id保证是唯一的(至少在一个组内),那么不要使用distinct I prefer sum() but you can also use count() :我更喜欢sum()但你也可以使用count()

select date, name, type_cd, type 
       count(CASE WHEN act_cd in (1) THEN key_id END) as count_of_AB,
       count(CASE WHEN act_cd in (2) THEN key_id END) as count_of_CD
from table 
where act_cd in (1, 2) and
      type in ('MR','LP')
group by date, name, type_cd, type;

just a minor bug otherwise your query is ok, you need to pass null in else to not count it:只是一个小错误,否则您的查询没问题,您需要通过 null 否则不计入:

select date,name,act_cd,type_cd, type 
count(distinct CASE WHEN act_cd in (1) THEN 1 ELSE NULL END) as count-of-AB,
count(distinct CASE WHEN act_cd in (2) THEN 1 ELSE NULL END) as count-of-CD
FROM table 
where act_cd in (1,2)
and type in (MR,LP)
group by date,name,act_cd,type_cd,type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM