Postgresql - Data is in the following format
I am currently using the following SQL to get me all the booking dates set for a property
However, in a single day, when a user changes booking dates for a property several times then multiple entries are created which inflate the aggregate result.
Can I apply ifelse logic to overlapping booking_dates (highlighted in red and yellow) to pick only the booking dates with max (updated_at)(highlighted in yellow)?
i.e. Booking start_date and End_Date shown in red and yellow are overlapping. If I applied the following SQL then my sum will be incorrect. So, I only need to consider the booking dates when updated_at is max.
WITH X AS (SELECT distinct booking_end_date, booking_start_date, P_id
FROM booking)
SELECT sum(X.booking_end_date-X.booking_start_date) as Available, X.P_id
from X
group by X.P_id
Aucun commentaire:
Enregistrer un commentaire