代码之家  ›  专栏  ›  技术社区  ›  Michu93

按值为“X”或“Y”的id分组

  •  0
  • Michu93  · 技术社区  · 6 年前

    桌子 bo_sip_cti_event_day 有列: uuid, hangup_clause 但我有很多相同的 uuid 在许多记录中 hangup_clause 例如: a, ORIGINATOR_CANCEL , a, NO_ANSWER , a, ALLOTTED_TIMEOUT

    我必须得到所有 uuid公司 ORIGINATOR_CANCEL NO_ANSWER 同样的 .

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Thorsten Kettner    6 年前

    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。