如何在rownum之间选择数据

时间:2017-11-02 15:19:02

标签: sql postgresql

拥有此小提琴中的数据my query fiddle

当我在其已经包含的数据之间尝试例如在1和5之间时,选择具有1到6之间的rownum的数据包括,如何做到这一点? 结果必须是:

2013    2013-12-24 09:02:38.000000  1100007 1937-06-17  1
2013    2013-12-24 09:02:38.000000  1100008 1937-06-17  2
2013    2013-12-24 09:02:38.000000  1100013 1937-06-17  3
2013    2013-12-24 09:02:38.000000  1100016 1937-06-17  4
2013    2013-12-24 09:02:38.000000  1100015 1937-06-17  5
2013    2013-12-24 09:02:38.000000  1100014 1937-06-17  6

1 个答案:

答案 0 :(得分:3)

如果我理解正确,您希望日期包含所有六个row_num值。假设您在给定日期没有row_num的重复值,您可以执行以下操作:

select t.*
from (select t.*,
             count(*) filter (where row_num between 1 and 6) over (partition by date) as cnt
      from test t
     ) t
where row_num between 1 and 6 and cnt = 6;

在早期版本的Postgres(或其他数据库)中:

select t.*
from (select t.*,
             sum(case when row_num between 1 and 6 then 1 else 0 end)  over (partition by date) as cnt
      from test t
     ) t
where row_num between 1 and 6 and cnt = 6;