I’m using postgres (postgis) and i want to perform a query that returns all rows that have a timestamp within an hour of each other.
What would the SQL statement look like?
The table looks like this:
- id { integer }
- name { character varying }
-
timestamp { timestamp with time zone }
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"
EDIT #1
Here’s a better example. (with help from Tometzky) All the results are within 1 hour of each other. That is, for every row give me any other rows that are within 1 hour:
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)
1 Answer