Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于迄今为止不同的“外键”计数的限制_Sql_Postgresql_Window Functions - Fatal编程技术网

Sql 基于迄今为止不同的“外键”计数的限制

Sql 基于迄今为止不同的“外键”计数的限制,sql,postgresql,window-functions,Sql,Postgresql,Window Functions,我有一个由两个表组成的联合体“站点”和“卡车”,按距离排序。记录集看起来像: 我需要获取所有行,直到我从第1行开始达到特定的n个唯一公司ID 因此,如果我得到如下结果: 然后我可以进行一个简单的查询,例如: SELECT * FROM union_recordset where distinct_company_id_count_so_far < (3 + 1); -- where n = 3 并得到预期的结果: 如果数据库支持countdistinct作为窗口函数: select ur

我有一个由两个表组成的联合体“站点”和“卡车”,按距离排序。记录集看起来像:

我需要获取所有行,直到我从第1行开始达到特定的n个唯一公司ID

因此,如果我得到如下结果:

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

SELECT * FROM union_recordset where distinct_company_id_count_so_far < (3 + 1);
-- where n = 3
并得到预期的结果:

如果数据库支持countdistinct作为窗口函数:

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( (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;

您可以选择您的公司,然后将其加入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命令。

您已使用不同的数据库标记了该问题。您应该只使用实际使用的数据库进行标记。对于此类问题,最好在中构建表和示例数据,这样您和回答问题的人就可以轻松地测试解决方案。子查询选择ur.*,按公司划分的分区上的行数按距离排序,因为union记录集ur中的seqnum未按距离返回有序列表。它仅按公司id排序,这进一步导致父查询出现问题。谢谢你的帮助@拉吉。我不明白。子查询中的顺序无关紧要。如果希望结果按特定顺序排列,则对外部查询使用order by。@RAJ。你似乎有很多很多公司都有相同的距离。它们都是一次清点的。我的要求是它们只能清点一次。也就是说,每个公司id只能从按距离排序的记录集中计算一次。@RAJ。您的数据中有数千家公司,它们之间的距离完全相同。它们都只被计算一次。在您的数据中,这些看起来像测试公司,所以您可能只想将它们过滤掉。