配置单元sql:具有多个条件计数

配置单元sql:具有多个条件计数,sql,hive,Sql,Hive,如何在拥有count子句时实现多个条件?例如prob=1时的情况,然后使用countcust_id而不是需要or条件时的情况 select .. from .. where ... group by prod... having prod=1 and count(cust_id)<=3 OR prod=0 and count(cust_id)<=2 我建议下一种解决方案,如果prod只是表中的一列,并且通过countcust_id,您希望统计表中每个cust_

如何在拥有count子句时实现多个条件?例如prob=1时的情况,然后使用countcust_id而不是需要or条件时的情况

 select  .. 
 from  ..  
 where ...
 group by prod...
 having prod=1 and count(cust_id)<=3
 OR prod=0 and count(cust_id)<=2

我建议下一种解决方案,如果prod只是表中的一列,并且通过countcust_id,您希望统计表中每个cust_id的出现次数:

select   cust_id
         , count(*)

from     <table_name>
where    prod=1
having   count(*) <= 3  

union

select   cust_id
         , count(*)

from     <table_name>
where    prod=0
having   count(*) <= 2  

请提供一些具有所需输出的数据示例。这似乎是正确的,但您需要确保prod位于组中。@GordonLinoff…正确,谢谢。。。根据您的建议更新答案