到目前为止,基于COUNT个DISTINCT`foreign_key`的限制

时间:2019-07-20 10:19:56

标签: sql postgresql window-functions

我有2个表“ sites”和“ trucks”的联合(按distance排序)。记录集如下:

enter image description here

我需要获取所有行,直到从第1行开始达到特定数量(n)的唯一company_id。

所以,如果我得到如下信息:enter image description here

然后我可以进行一个简单的查询,例如:

SELECT * FROM union_recordset where distinct_company_id_count_so_far < (3 + 1);
-- where n = 3

并获得以下期望的结果:

enter image description here

2 个答案:

答案 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命令。