Sql 从表中选择时间戳相隔不超过一小时的项目
我使用的是postgres postgis,我想执行一个查询,返回彼此间隔一小时内具有时间戳的所有行 SQL语句是什么样子的 该表如下所示:Sql 从表中选择时间戳相隔不超过一小时的项目,sql,postgresql,Sql,Postgresql,我使用的是postgres postgis,我想执行一个查询,返回彼此间隔一小时内具有时间戳的所有行 SQL语句是什么样子的 该表如下所示: $ select * from test; id | t ----+------------------------------- 9 | 2011-07-15 18:31:01.059487+02 10 | 2011-07-15 18:31:01.55044+02 11 | 2011-0
$ select * from test;
id | t
----+-------------------------------
9 | 2011-07-15 18:31:01.059487+02
10 | 2011-07-15 18:31:01.55044+02
11 | 2011-07-15 18:31:01.850583+02
12 | 2011-07-15 18:31:02.064435+02
13 | 2011-07-15 18:31:02.333449+02
14 | 2011-07-15 18:31:02.727461+02
15 | 2011-07-15 18:31:03.279447+02
16 | 2011-07-15 18:31:03.642454+02
17 | 2011-07-15 18:33:34.910252+02
18 | 2011-07-15 18:33:35.995455+02
19 | 2011-07-15 18:33:36.45246+02
(11 rows)
$ select id, lastid
from (
select id,
t-lag(t) over (order by t) as lag,
lag(id) over (order by t) as lastid from test
) as _
where lag < '1 second';
id | lastid
----+--------
10 | 9
11 | 10
12 | 11
13 | 12
14 | 13
15 | 14
16 | 15
19 | 18
(8 rows)
id{integer}
名称{字符变化}
时间戳{时区时间戳}
id name timestamp
---+----------+--------------------------------
1 one "2010-09-24 21:10:39.515+00"
2 two "2010-09-16 09:21:09.362+00"
3 three "2010-07-08 00:00:46.549+00"
编辑1
这里有一个更好的例子。在托梅茨基的帮助下,所有结果都在1小时内完成。也就是说,对于每一行,请给出1小时内的任何其他行:
select * from myTable order by t
id | t
----+-------------------------------
9 | 2011-07-15 18:20:20.05+02
10 | 2011-07-15 19:05:00.05+02
11 | 2011-07-15 19:40:20.05+02
13 | 2011-07-15 20:31:01.05+02
14 | 2011-07-15 20:35:11.05+02
(5 rows)
result of needed query:
id | matchid | origTime | matchTime
----+----------+----------------------------+------------------------------
9 | 10 | 2011-07-15 18:20:20.05+02 | 2011-07-15 19:05:00.05+02
10 | 9 | 2011-07-15 19:05:00.05+02 | 2011-07-15 18:20:20.05+02
10 | 11 | 2011-07-15 19:05:00.05+02 | 2011-07-15 19:40:20.05+02
11 | 10 | 2011-07-15 19:40:20.05+02 | 2011-07-15 19:05:00.05+02
11 | 13 | 2011-07-15 19:40:20.05+02 | 2011-07-15 20:31:01.05+02
11 | 14 | 2011-07-15 19:40:20.05+02 | 2011-07-15 20:35:11.05+02
13 | 11 | 2011-07-15 20:31:01.05+02 | 2011-07-15 19:40:20.05+02
13 | 14 | 2011-07-15 20:31:01.05+02 | 2011-07-15 20:35:11.05+02
14 | 11 | 2011-07-15 20:35:11.05+02 | 2011-07-15 19:40:20.05+02
14 | 13 | 2011-07-15 20:35:11.05+02 | 2011-07-15 20:31:01.05+02
(10 rows)
大概是这样的:
$ select * from test;
id | t
----+-------------------------------
9 | 2011-07-15 18:31:01.059487+02
10 | 2011-07-15 18:31:01.55044+02
11 | 2011-07-15 18:31:01.850583+02
12 | 2011-07-15 18:31:02.064435+02
13 | 2011-07-15 18:31:02.333449+02
14 | 2011-07-15 18:31:02.727461+02
15 | 2011-07-15 18:31:03.279447+02
16 | 2011-07-15 18:31:03.642454+02
17 | 2011-07-15 18:33:34.910252+02
18 | 2011-07-15 18:33:35.995455+02
19 | 2011-07-15 18:33:36.45246+02
(11 rows)
$ select id, lastid
from (
select id,
t-lag(t) over (order by t) as lag,
lag(id) over (order by t) as lastid from test
) as _
where lag < '1 second';
id | lastid
----+--------
10 | 9
11 | 10
12 | 11
13 | 12
14 | 13
15 | 14
16 | 15
19 | 18
(8 rows)
一个小时之内?如:13:00、13:59和14:35属于同一组?哪个版本的博士后?从8.4开始,Postgres具有窗口功能,这在这里会有所帮助。@OMG:所以对于行id 2,8:21:09和10:21:09之间的任何内容都将匹配。一定是有问题,但不确定是什么问题。例如,如果9是10的1秒,那么10也是9的1秒。请看我的编辑1我在你的例子的基础上放了一个例子,希望它能更好地解释我需要什么。在一个例子之前,你不清楚你想要什么@Clodoado给了你正确的答案。抱歉+1:对于你的努力和我的错误。但是请记住,你需要一个ts索引来获得合理的性能。谢谢,但是我得到了这个查询的所有可能的组合。这是一行匹配我的所有行,以此类推。我得到的是相隔多年的比赛。