row_number() and partition

quote
A table has three columns, id, name, score, and now we want to get the two courses with the highest score for each person


create temp table tmp_xxx
as
select 1 id,'A' name,80 grade
UNION ALL
select 2,'A',90
UNION ALL
select 3,'A',60
UNION ALL
select 4,'B',100
UNION ALL
select 5,'B',90
UNION ALL
select 6,'B',50;



select id,name,grade
from
(
select id,name,grade, row_number() over (partition by name order by grade desc) as RN
from
tmp_xxx
) tmp
where RN<=2;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326697863&siteId=291194637