【问题标题】:SQL Count based on like statement and Group bySQL Count 基于 like 语句和 Group by
【发布时间】:2021-02-03 10:02:21
【问题描述】:

我有一个类似于以下结构的表格。我需要根据 MessageValue 列的值获取失败、成功和部分成功的计数。如果 Messagevale 列包含“失败”,则为失败,“成功”被视为成功,“部分成功”为部分失败。所以我需要在计数中检查 LIKE 标准,并且我还需要根据日期。我尝试使用 Group by 计数,但没有得到我需要的。

Date                   MessageValue
-----------------------------------------------
10/18/2020             Process failed some text here
10/18/2020             Process success some text here
10/19/2020             Partial success some text here
10/19/2020             Process failed some text here
10/19/2020             Process failed some text here
10/19/2020             Process partial success some text here
10/20/2020             Process partial success some text here
------------------------------------------------

所以输出应该如下所示。

Date                   Success      Failure     Partial Failure
-----------------------------------------------------------------
10/18/2020               1             1            0
10/19/2020               1             2            1
10/20/2020               0             0            1

【问题讨论】:

  • sum(case when col like '%failure%' then 1 else 0 end)

标签: sql count pivot aggregate-functions sql-like


【解决方案1】:

您可以对like 条件进行条件聚合。这似乎可以满足您的要求:

select
    date,
    sum(case when col like '%success%' and col not like '%partial success%' then 1 else 0 end) success,
    sum(case when col like '%failed%' then 1 else 0 end) failure,
    sum(case when col like '%partial failed%' then 1 else 0 end) partial_success
from mytable
group by date

【讨论】:

    猜你喜欢
    • 2012-10-20
    • 2012-07-09
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多