sql-基于不同列项的计数

n6lpvg4x  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(195)

我有一个表格,包括一个团队和一个风险列。风险可以是严重的、高的、中的或低的。队伍各不相同。
我想创建一个查询,在这个查询中,我将得到每个团队的风险计数=。e、 克-

|Team|Count_Critical|Count_High|Count_Medium|Count_Low| (Each team will be only in a single row).
      |T-1 |    3         |   5      |   8        |    5    |
      |T-2 |    1         |   0      |   0        |    89   |

请告诉我怎么做。

lymgl2op

lymgl2op1#

您似乎想要条件聚合:

select team,
       sum(case when risk = 'Critical' then 1 else 0 end) as critical,
       sum(case when risk = 'High' then 1 else 0 end) as high,
       sum(case when risk = 'Medium' then 1 else 0 end) as medium,
       sum(case when risk = 'Low' then 1 else 0 end) as low
from t
group by team;

相关问题