1
GOAL
I’d like to make a SELECT
within a range of 1 hour in 1 hour
DATA TABLE
CREATE TABLE t (
t_id INT PRIMARY KEY,
time TIMESTAMP
);
INSERT INTO t VALUES (1, '2019-10-28 08:00:00');
INSERT INTO t VALUES (2, '2019-10-28 08:30:00');
INSERT INTO t VALUES (3, '2019-10-28 09:00:00');
INSERT INTO t VALUES (4, '2019-10-28 09:30:00');
INSERT INTO t VALUES (5, '2019-10-28 10:00:00');
INSERT INTO t VALUES (6, '2019-10-28 10:30:00');
QUERY
SELECT
t_id,
MIN(time)
FROM
t
WHERE
date_trunc('day', time) = current_date
GROUP BY
t_id,
date_trunc('hour', time)
OUTPUT
t_id min
4 2019-10-28 09:30:00
5 2019-10-28 10:30:00
1 2019-10-28 08:00:00
2 2019-10-28 08:30:00
3 2019-10-28 09:00:00
6 2019-10-28 10:00:00
EXPECTED OUTPUT
t_id min
1 2019-10-28 08:00:00
3 2019-10-28 09:00:00
5 2019-10-28 10:00:00
Note: Reworded question
For your example of data and expected result, you could use Where Extract('minute' from time) = 0 that solves.
– Benilson
I’ll try it, thanks buddy!!
– Luis Henrique