sql语句,如何对同一列数据中不同的值,计数

如何在一张表中,把高、中、低的多少统计在表中
2024-10-31 17:47:53
推荐回答(2个)
回答1:

select bg_severity,count(*) as row_count
from 表名 group by bg_severity

结果:
bg_severity row_count

1-低 xx
2-中 xx
1-高 xx

select sum(case when bg_severity = N'1-低' then 1 else 0 end) as [1-低],
sum(case when bg_severity = N'2-中' then 1 else 0 end) as [2-中],
sum(case when bg_severity = N'3-高' then 1 else 0 end) as [3-高]
from 表名
结果:
1-低 |2-中 |3-高
xx |xx | xx

回答2:

select bg_severity,count(*) from 表名 group by bg_severity