I saw other posts about using the median () window function in Redshift , but how would you use it with a query that has a group at the end?
For example, suppose the table rate is:
Course | Subject | Num_Students ------------------------------- 1 | Math | 4 2 | Math | 6 3 | Math | 10 4 | Science | 2 5 | Science | 10 6 | Science | 12
I want to get the average number of students for each subject of the course. How to write a query that gives the following result:
Subject | Median ----------------------- Math | 6 Science | 10
I tried:
SELECT subject, median(num_students) over () FROM course GROUP BY 1 ;
But it lists all the cases of the subject and the same average for topics such as (this is fake data, so the actual value that he returns is not 6, but simply shows it the same way for all items):
Subject | Median ----------------------- Math | 6 Math | 6 Math | 6 Science | 6 Science | 6 Science | 6
source share