By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table test
(time int, location varchar(1));
insert into test values
(10,'A'),
(1,'B'),
(2,'C'),
(3,'D'),
(4,'E');
Records: 5 Duplicates: 0 Warnings: 0
SELECT
t.time,
t.location
FROM test t
INNER JOIN (SELECT MAX(time) AS time FROM test) m
ON t.time BETWEEN m.time - 7 AND m.time
ORDER BY t.time DESC; -- remove the sort if not required
time | location |
---|---|
10 | A |
4 | E |
3 | D |
WITH max_time AS
(SELECT MAX(time) AS time FROM test)
SELECT
t.time,
t.location
FROM test t
INNER JOIN max_time m
ON t.time BETWEEN m.time - 7 AND m.time
ORDER BY t.time DESC; -- remove the sort if not required
time | location |
---|---|
10 | A |
4 | E |
3 | D |