GROUP BY id具有“ X”之类的值或“ Y”之类的值

时间:2018-08-20 12:05:06

标签: postgresql group-by having

bo_sip_cti_event_day的列为:uuid, hangup_clause,但在许多记录中,我与其他uuid的记录相同,hangup_clause也很多,例如:a, ORIGINATOR_CANCEL,{{1 }},a, NO_ANSWER

我必须获取所有a, ALLOTTED_TIMEOUT,其中hangup_cause为uuid,而ORIGINATOR_CANCEL代表相同的NO_ANSWER

到目前为止,我尝试过: uuid 但随后错误提示select uuid from bo_sip_cti_event_day group by uuid having hangup_cause like 'ORIGINATOR_CANCEL' and hangup_cause like 'NO_ANSWER'必须在group by子句中或在聚合函数中。

1 个答案:

答案 0 :(得分:1)

HAVING子句中,处理聚合。您情况下的条件聚合:

select uuid
from bo_sip_cti_event_day 
group by uuid
having count(case when hangup_cause = 'ORIGINATOR_CANCEL' then 1 end) > 0
   and count(case when hangup_cause = 'NO_ANSWER' then 1 end) > 0;

这将为您提供所有两个挂断子句都存在的UUID。