I'm trying to find a way to query for the first day and last day of the previous week with postgresql.
I've found the answer for both mysql ,sql server and other but couldn't find it for PSQL.
I've been trying using current_date and intervals 1 week
or - integer '7'
( BETWEEN current_date - INTERVAL '1 weeks' and current_date
) but it just gives me 1 week back from the current day and not the first or last day in store for the previous week.
I'm looking for something dynamic : The first day of the previous week could be tuesday and the last day thursday it's not always monday and friday depends what has been stored for the previous week.
I need to be able to retreive a result for the 1st day of the previous week aswell as the last day so it's going to be 2 queries i believe.
edit : My main issue is that most answer around the same problem use current_date / now() and subtract a fix number of days to retrieve the first day of the previous week. But the query should be dynamic it won't be done on the same day every week - so the subtraction of days will be different depending on which day the query is performed - AND the first day of previous week that i have in store isn't always monday and last day isn't always friday. Sometimes i'll only have 3 days worth of data for a week so the first day will be tuesday and last day thursday and i wouldn't want to return NULL for monday and Null for friday
edit 2 :
dummy data looks like that :
id | date
1 monday(w1)
2 tuesday(w1)
3 wednesday(w1)
4 thursday(w1)
5 friday(w1)
6 tuesday(w2)
7 wednesday(w2)
8 friday(w2)
9 monday(w3)
10 tuesday(w3)
--query ran here--
Obviously my dates are stored as actual date format ( 2019-10-24 ) but on the date of the query ( hypnotically wednesday ) i'd want the first item of the previous week to be id 6 and the last item to be id 8
integer '7'
can be simplified to7
– a_horse_with_no_name