在具有count子句的情况下如何实现多个条件?例如case when prob=1 then having count(cust_id)<=3 or case when prob=0 then having count(cust_id)<=2
提前谢谢!
答案 0 :(得分:1)
代替您似乎需要or条件的情况
select ..
from ..
where ...
group by prod...
having prod=1 and count(cust_id)<=3
OR prod=0 and count(cust_id)<=2
答案 1 :(得分:0)
如果prod只是表中的一列,并且要通过“ count(cust_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