gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:47:01 25 4
gpt4 key购买 nike

假设有一个表,其中包含有关日期交易的数据:

+----+------------------+
| 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 次的所有交易。所以在上表中它将是(交易 ID):4、10、11

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

最佳答案

我觉得不能用窗口函数实现,用子语句:

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

关于Postgresql:获取给定数据范围内的事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46765818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com