需要帮助编写SQL Server查询

时间:2016-09-08 16:25:40

标签: sql-server tsql

我有一张包含一些数据的表格..学校中每个学生的百分比低于列......

Class | name | Percentage( % ) 

我需要一个SQL查询来计算每个班级中学生数量低于Diff%slabs的数量。未编写考试的学生将在源表的Percentage列中显示NULL。

class| 0-10% | 10%-20% | 20%-30% | >30% | Not written ( NULLS in source table)

有人可以帮我写这个查询吗?

1 个答案:

答案 0 :(得分:0)

我猜这有助于你..

SELECT 
CLASS, 
[0-10%]= COUNT(CASE WHEN PERCENTAGE BETWEEN 0 AND 10 THEN 1 END),
[11-20%]= COUNT(CASE WHEN PERCENTAGE BETWEEN 11 AND 20 THEN 1 END),
[21-30%]= COUNT(CASE WHEN PERCENTAGE BETWEEN 21 AND 30 THEN 1 END),
[>30%]= COUNT(CASE WHEN PERCENTAGE >30 THEN 1 END),
[NOT WRITTEN]= COUNT(CASE WHEN PERCENTAGE IS NULL THEN 1 END)
FROM [Tablename] GROUP BY CLASS