I have a table that has daily data and thousands of rows per day:
create table my_table(visitors_count int, dt date);
select setseed(.42);
insert into my_table
select (random()*100)::int,
current_date+(-random()*14)::int
from generate_series(1,42000);
However, some days are not present due to holidays:
delete from my_table where dt = '2024-03-08';
I need a query that would check if the initial date has data, and if it does, retrieves the data for the previous 5 days.
Example: If I put in today’s date 2024/3/12
, I would need to
-
Check if
2024/3/12
has data. -
If yes, retrieve data for March
7
,8
,9
,10
,11
and12
:2024-03-07 2024-03-08 2024-03-09 2024-03-10 2024-03-11 2024-03-12 -
However, if let’s say the 8th is a holiday and there was no data, I would need data for
6
,7
,9
,10
,11
,12
:2024-03-06 2024-03-07 2024-03-09 2024-03-10 2024-03-11 2024-03-12
2
Answers
Set up a window to order by date descending and use
dense_rank()
to assign the same numbers to the same dates – it will skip the empty ones. Then ask for only those up to 5 days back: demoUsing a CTE, you can get the last 5 dates preceding your date of interest using
limit 6
and ordering by the date. And only returning results when the date of interest exists in the table. Then select all data points from your table that match those 6 dates (if any): demo