SQL视图:如何创建视图以获得五个不同类别的前5个最高值?

问题描述 投票:0回答:1

我有一张桌子productspricescolours列。我需要一个视图来获得每种颜色的五大最昂贵的产品。我该怎么办?

sql greatest-n-per-group
1个回答
1
投票

这样做:

 select * from (select a.*,rank() over (partition by colour order by price desc) rk  from 
 tableaname a) where rk<6;

而是这个:

CREATE VIEW view_name AS    select * from (select a.*,rank() over (partition 
by colour order by price desc) rk  from 
tableaname a) where rk<6;
© www.soinside.com 2019 - 2024. All rights reserved.