编写sql语句,计算每个学校、每个年级、每个班的各科平均分、最高分、及格人数,其中及格分数线为90分
时间: 2024-10-18 12:19:25 浏览: 51
编写这样的SQL语句涉及到多级聚合和条件判断,假设我们有`grades`表,包含以下字段:`school_id`, `class_id`, `grade`, `subject`, `score`。下面是一个示例结构的SQL查询:
```sql
WITH scores_per_class AS (
SELECT
school_id,
class_id,
grade,
subject,
AVG(score) OVER (PARTITION BY school_id, class_id, grade, subject) as avg_score,
MAX(score) OVER (PARTITION BY school_id, class_id, grade, subject) as max_score,
COUNT(CASE WHEN score >= 90 THEN 1 END) OVER (PARTITION BY school_id, class_id, grade) as passing_students
FROM grades
)
SELECT
s.school_id,
s.class_id,
s.grade,
s.subject,
s.avg_score,
s.max_score,
s.passing_students
FROM scores_per_class s;
```
这个查询首先通过窗口函数(Window Function)计算了每个班级、学科的每个年级的平均分(avg_score)、最高分(max_score),以及及格的学生人数(passing_students)。这里的CASE WHEN语句用于检查分数是否大于等于90,并统计符合条件的数量。
阅读全文