I work on an hotel app, and I am trying to query my sql db for the available rooms between 2 dates.
The table rate
looks like this:
room_id | date | unavailable | price |
---|---|---|---|
1 | 16/08/2022 | false | 10 |
1 | 17/08/2022 | null | 20 |
1 | 18/08/2022 | null | 30 |
2 | 16/08/2022 | false | 10 |
2 | 17/08/2022 | true | 20 |
2 | 18/08/2022 | null | 30 |
3 | 16/08/2022 | false | 10 |
3 | 17/08/2022 | false | 20 |
3 | 19/08/2022 | false | 30 |
I am looking for the rooms available between from
= 16/08/2022
and to
= 18/08/2022
, i.e. the room_id
s
- with at least one row for every date between from and to
- where none of these row has
unavailable = true
expected output
room_id |
---|
1 |
2
Answers
use a not exists
You can use a
GROUP BY
where you count the available dates and compare it to the dates you nned,more complex pattern would need more sophisticated algorithms
db<>fiddle here
every cimlication has its price in execution time
db<>fiddle here