假设我有一个带有数字列的表(我们称之为“分数”)。
我想生成一个计数表,显示每个范围内分数出现的次数。
例如:
score range | number of occurrences
-------------------------------------
0-9 | 11
10-19 | 14
20-29 | 3
... | ...
在此示例中,有 11 行的分数在 0 到 9 的范围内,14 行的分数在 10 到 19 的范围内,3 行的分数在 20-29 的范围内。
有一个简单的方法来设置这个吗?你有什么建议吗?
得票最高的答案在 SQL Server 2000 上都不正确。也许他们使用的是不同的版本。
以下是它们在 SQL Server 2000 上的正确版本。
select t.range as [score range], count(*) as [number of occurences]
from (
select case
when score between 0 and 9 then ' 0- 9'
when score between 10 and 19 then '10-19'
else '20-99' end as range
from scores) t
group by t.range
or
select t.range as [score range], count(*) as [number of occurrences]
from (
select user_id,
case when score >= 0 and score< 10 then '0-9'
when score >= 10 and score< 20 then '10-19'
else '20-99' end as range
from scores) t
group by t.range
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)