I want to get the most recent records for every customer and also accept/get records which are 1 hour older to the most recent record.
I am currently getting the most recent records for every customer using this SQL query –
x
SELECT sub.customer, sub.date_field FROM(
SELECT customer, date_field,
RANK() OVER
(
PARTITION by customer
ORDER BY date_field desc
) AS rownum
FROM TableA) sub
WHERE rownum = 1
What I need is to get the most recent records and records which are an hour older than the recent records for every customer.
Advertisement
Answer
Hmmm . . . You can get the maximum timestamp and use date comparisons:
select a.*
from (select a.*,
max(timestamp) over (partition by customer) as max_timestamp
from tableA a
) a
where timestamp >= max_timestamp - interval '1 hour'
order by customer, timestamp desc;