PostgreSQL:选择每组中的前三名

时间:2014-12-11 05:05:31

标签: sql postgresql group-by greatest-n-per-group

以下是我的示例表和行

create table com (company text,val int);


insert into com values ('com1',1),('com1',2),('com1',3),('com1',4),('com1',5);

insert into com values ('com2',11),('com2',22),('com2',33),('com2',44),('com2',55);

insert into com values ('com3',111),('com3',222),('com3',333),('com3',444),('com3',555);

我想获得每家公司的前3名,预期产出是:

company  val
---------------
com1     5
com1     4
com1     3
com2     55
com2     44
com2     33
com3     555
com3     444
com3     333

2 个答案:

答案 0 :(得分:10)

试试这个:

SELECT company, val FROM 
(
    SELECT *, ROW_NUMBER() OVER (PARTITION BY 
             company order by val DESC) AS Row_ID FROM com
) AS A
WHERE Row_ID < 4 ORDER BY company

- Quick Demo Here...

答案 1 :(得分:4)

从v9.3开始,你可以进行横向连接

select distinct com_outer.company, com_top.val from com com_outer
join lateral (
    select * from com com_inner
    where com_inner.company = com_outer.company
    order by com_inner.val desc
    limit 3
) com_top on true
order by com_outer.company;

might be faster但是,当然,您应该专门针对您的数据和用例测试性能。