Postgresql:获取给定数据范围内的事务

Postgresql: Getting transactions in given data range

让我们假设,有一个 table 其中包含有关交易的数据,日期为:

+----+------------------+
| id |    localdate     |
+----+------------------+
|  1 | 2017-10-16 10:00 |
|  2 | 2017-10-16 10:01 |
|  3 | 2017-10-16 10:02 |
|  4 | 2017-10-16 10:06 |
|  5 | 2017-10-16 10:20 |
|  6 | 2017-10-16 10:26 |
|  7 | 2017-10-16 10:45 |
|  8 | 2017-10-16 10:46 |
|  9 | 2017-10-16 10:47 |
| 10 | 2017-10-16 10:48 |
| 11 | 2017-10-16 10:49 |
+----+------------------+

现在,用户可以在应用程序中设置两个过滤器:数据范围和交易限制。用户应该看到在给定数据范围内发生次数超过交易限制的所有交易的计数。 例如,当交易限制设置为 3 且数据范围设置为 15 分钟时,我需要获取在任何 15 分钟内至少发生 4 次的所有交易。 所以在上面 table 它将是 (transaction ids): 4, 10, 11

我尝试使用 window 函数来实现,但无法完全实现。有人知道怎么做吗?

我认为不能通过window函数实现,使用子语句:

with data as (select unnest(tim)::timestamp tim from (select 
          array[
            '2017-10-16 10:00',
            '2017-10-16 10:01',
            '2017-10-16 10:02',
            '2017-10-16 10:06',
            '2017-10-16 10:20',
            '2017-10-16 10:26',
            '2017-10-16 10:45',
            '2017-10-16 10:46',
            '2017-10-16 10:47',
            '2017-10-16 10:48',
            '2017-10-16 10:49'
          ] tim) a)
       select tim from (   
        select a.tim, (select count(1) from data where tim >= a.tim - interval '15 minutes' and tim <= a.tim and tim <> a.tim) + 1 cnt from (
         select * from data
        ) a 
      ) a where cnt >= 4