1

我有一个包含测试分数列表的表格,例如,分数为 30 分。

我想通过计算得分在某个百分比范围内的人数来呈现数据。

例如:

Total   % Bracket
-----------------
5       95-100%
15      90-94%   
20      85-89%
17      80-84%
15      75-79%
etc

我想过先计算每个百分比括号所需的分数,然后以SUM(CASE ....某种方式进行计算,但我似乎迷失了方向。

4

3 回答 3

1

没有时间测试这个,但有些东西很长......

select t.pcbracket as [% Bracket], count(*) as [NumWithMark]
from (
  select case  
    when mark between 0 and 9 then ' 0- 9'
    when mark between 10 and 19 then '10-19'
    when mark between 20 and 29 then '20-29'
    when mark between 30 and 39 then '30-39'
    else '40-100' end as pcbracket
  from testresults) t
group by t.pcbracket
于 2013-10-02T10:35:30.513 回答
1

您可以使用 CASE WHEN 但在这种情况下,您将错过记录数为 0 的间隔。因此,您可以通过以下方式进行操作:

select t2.start,t2.finish, count(t.score) from t
RIGHT JOIN 
(
select 0 as start, 4 as finish
union all
select 5 as start, 9 as finish
union all
select 10 as start, 14 as finish
union all
.......
union all
select 95 as start, 99 as finish  
) as t2 on t.score between t2.start and t2.finish

group by t2.start,t2.finish
order by  t2.start

这是 SQLFiddle 演示

于 2013-10-02T10:55:16.073 回答
0

我会使用嵌套查询来解决这个问题。内部查询我将使用简单的按百分比分组,然后在它之上我将使用案例来指定百分比范围并获取输出。

于 2013-10-02T10:30:55.223 回答