postgresql – 在AWS Redshift中使用Group By计算中值
发布时间:2020-12-13 18:05:17 所属栏目:百科 来源:网络整理
导读:我已经看过 other posts about using the median() window function in Redshift了,但你怎么用它到最后有一个组的查询呢? 例如,假设表课程: Course | Subject | Num_Students------------------------------- 1 | Math | 4 2 | Math | 6 3 | Math | 10 4 |
我已经看过
other posts about using the median() window function in Redshift了,但你怎么用它到最后有一个组的查询呢?
例如,假设表课程: Course | Subject | Num_Students ------------------------------- 1 | Math | 4 2 | Math | 6 3 | Math | 10 4 | Science | 2 5 | Science | 10 6 | Science | 12 我想得到每门课程的学生中位数.我如何编写一个给出以下结果的查询: Subject | Median ----------------------- Math | 6 Science | 10 我试过了: SELECT subject,median(num_students) over () FROM course GROUP BY 1 ; 但是它列出了主题的每一次出现以及相同主题的相同中位数数字(这是假数据,因此它返回的实际值不是6,但只显示所有主题的相同): Subject | Median ----------------------- Math | 6 Math | 6 Math | 6 Science | 6 Science | 6 Science | 6
您只需要删除它的“over()”部分.
SELECT subject,median(num_students) FROM course GROUP BY 1; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |