首页 > 解决方案 > SQL COUNT 忽略列

问题描述

我对 SQL 查询有疑问:

我从查询中得到以下结果:

select distinct eb.event_type_id, eb.status from eid.event_backlog eb order by 1

|event_type_id|status    |
|-------------|----------|
|1            |SUCCESS   |
|2            |SUCCESS   |
|2            |ERROR     |
|3            |SUCCESS   |
|3            |ERROR     |
|4            |SUCCESS   |

我想获得这个结果做一个不同的状态:

|event_type_id|count  |
|-------------|-------|
|1            |1      |
|2            |2      |
|3            |2      |
|4            |1      |

但我看到获得此结果的唯一方法是执行以下查询:

select
    eb.event_type_id,
    count(1)
from
    (
    select
        distinct eb.event_type_id, eb.status
    from
        eid.event_backlog eb
    order by
        1) eb
group by
    eb.event_type_id

我不喜欢使用嵌套查询,还有另一种方法可以获得我想要的吗?

标签: sqlcount

解决方案


简单地说count(distinct eb.status),即

select
    eb.event_type_id,
    count(distinct eb.status)
from eid.event_backlog eb
group by
    eb.event_type_id

推荐阅读