selectavg(m.rating) from (select m1.rating, row_number() over(orderby m1.rating) as row_num, count(*) over() as cnt from movie_ratings m1) m where m.row_num in (floor(cnt /2) +1, casewhenmod(cnt, 2) =0then cnt /2elsefloor(cnt /2) +1end); --14
1 2 3 4 5 6
selectavg(m.rating) from (select m1.rating, row_number() over(orderby m1.rating) as row_num, count(*) over() as cnt from movie_ratings m1) m where m.row_num between cnt /2and cnt /2+1;
1 2 3 4 5 6 7 8 9
--11, 12, 12, 13, 15, 16, 19, 20 selectavg(m.rating) from (select m1.rating, row_number() over(orderby m1.rating) as row_num1, --11, 12, 12, 13, 15, 16, 19, 20 row_number() over(orderby m1.rating desc) as row_num2, --20, 19, 16, 15, 13, 12, 12, 11 count(*) over() as cnt from movie_ratings m1) m where row_num1 = round(cnt /2) --13 or row_num2 = round(cnt /2); --15