答案 0 :(得分:4)
您可以使用RANK
:
SELECT *
FROM (SELECT *, RANK() OVER(PARTITION BY class ORDER BY sales DESC) AS rnk
FROM tab) sub
WHERE rnk <=2;
答案 1 :(得分:1)
窗口函数density_rank也满足样本数据的预期结果条件
select * from
(select * , dense_rank() over(partition by class order by scores DESC ) as rn
from tablea
) t where rn<=2