sql - 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
我不喜欢使用嵌套查询,还有另一种方法可以获得我想要的吗?
解决方案
简单地说count(distinct eb.status)
,即
select
eb.event_type_id,
count(distinct eb.status)
from eid.event_backlog eb
group by
eb.event_type_id
推荐阅读
- python - 使用 qutip 对量子比特应用门?
- google-apps-script - 谷歌脚本如何通过时间触发器将光标移动到谷歌电子表格同一张表中的另一个单元格
- sql-server - 数据库中的浮点数显示斜线而不是点
- css - Material UI v1.0 如何让对话框背景透明?
- javascript - CSS3转换中的javascript layerX和event.layerX:翻译
- angular - apollo.watchQuery 导致 'ERROR TypeError: Object(...) is not a function'
- fonts - 如何将 Sublime 文本中的字体更改为“Fixed Semicondensed”?
- python - 如何使用 Google Colab 安装 vizdoom?
- ios - 如何获取 UIStackView 内的 UIView 的宽度值
- javascript - 无法在 Angular 上显示日期管道