我有一个包含考试结果的关系数据库,但是我只需要使用一个表查询:
我需要查询以计算每个科目参加考试的学生中位数(导致每个科目学生参加1个以上考试),然后显示哪个科目的中位数最高。
我对使用SQL(元数据库)计算中位数的知识非常有限,我首先使用了result_table.student_no count(*)函数和GROUP BY results_tbl.subject_code,但我不认为这是合适的方法
对于我需要的表中的每个主题代码
例如
# OF STUDENT WHO SAT EXAM = _ _ _ & MEDIAN # OF STUDENTS = _ _
(按DESC排序)
答案 0 :(得分:0)
鉴于您先前的问题是用SQL Server标记的,我将假设这就是您正在使用的内容。在这种情况下,您可以使用PERCENTILE_CONT()
或PERCENTILE_DISC()
例如
SELECT t.Col,
Median_Cont = PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY t.Col) OVER(),
Median_Disc = PERCENTILE_DISC(0.5) WITHIN GROUP(ORDER BY t.Col) OVER()
FROM (VALUES (1), (2), (3), (4)) AS t (Col);
礼物:
Col Median_Cont Median_Disc
--------------------------------------
1 2.5 2
2 2.5 2
3 2.5 2
4 2.5 2
要限制结果,您需要一个子查询:
SELECT t.Col
FROM ( SELECT t.Col,
Median_Disc = PERCENTILE_DISC(0.5)
WITHIN GROUP(ORDER BY t.Col) OVER()
FROM (VALUES (1), (2), (3), (4)) AS t (Col)
) AS t
WHERE t.Col = t.Median_Disc;
结果
Col
-----
2
对于您的特定示例,我认为您还需要添加PARTITION BY
以确保每个主题的中位数都被计算出来:
SELECT r.subject_code, r.score, r.Date
FROM ( SELECT r.*,
Median = PERCENTILE_DISC(0.5)
WITHIN GROUP(ORDER BY r.Score)
OVER(PARTITION BY r.Subject_Code)
FROM Results_tbl AS r
) AS r
WHERE r.Score = r.Median;