我有2个表“ sites”和“ trucks”的联合(按distance
排序)。记录集如下:
我需要获取所有行,直到从第1行开始达到特定数量(n)的唯一company_id。
然后我可以进行一个简单的查询,例如:
SELECT * FROM union_recordset where distinct_company_id_count_so_far < (3 + 1);
-- where n = 3
并获得以下期望的结果:
答案 0 :(得分:0)
如果您的数据库支持count(distinct)
作为窗口函数:
select ur.*,
count(distinct company_id) over (order by distance) as cnt
from union_recordset ur
order by distance;
如果没有,则可以计算第一次出现次数:
select ur.*,
sum(case when seqnum = 1 then 1 else 0 end) over (order by distance) as cnt
from (select ur.*,
row_number() over (partition by company_id order by distance) as seqnum
from union_recordset ur
) ur
order by distance;
在Postgres中,sum()
可以简化为:
sum( (seqnum = 1)::int ) over (order by distance) as cnt
然后要获取例如前三家公司的数字,您需要:
select ur.*
from (select ur.*,
sum( (seqnum = 1)::int ) over (order by distance) as cnt
from (select ur.*,
row_number() over (partition by company_id order by distance) as seqnum
from union_recordset ur
) ur
) ur
where cnt <= 3
order by distance;
答案 1 :(得分:0)
您可以选择您的公司,然后将其自身与agian一起加入以获取其他数据:
select ur.* from union_recordset ur join
(select distinct company_id from union_recordset order by distance limit 3) ur_d
on (ur.company_id = ur_d.company_id)
注意:PostgreSQL 8.1之后支持Limit
命令。