can anyone help me with my sql statement below. What i am trying to do is retrieve rows from 1 table where they do not appear within the date range of another table. The query below at the moment returns the row that is between the date range entered. How could i modify it so that the rows not between the date range in table 2 are returned from table 1? table 1 would be homes and table 2 would be bookings
SELECT homes.home_id, homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft,
listagg(features.feature_name, '\n') WITHIN GROUP(ORDER BY features.feature_name) features, home_type.type_name
FROM homes
INNER JOIN bookings ON bookings.home_id = homes.home_id
INNER JOIN home_feature ON homes.home_id = home_feature.home_id
INNER JOIN home_type ON home_type.type_code = homes.type_code
INNER JOIN features ON home_feature.feature_id = features.feature_id
WHERE bookings.booking_end < to_date('23-Jan-13')
OR bookings.booking_start > to_date('22-Jan-13')
GROUP BY homes.home_id, homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft, home_type.type_name
You will want to use a
LEFT JOINto return all rows fromhomesthat do not appear inbookings. I would also suggest moving thebookings.datefilter from theWHEREclause to theJOINcondition:Based on your comment, you can try a
NOT EXISTS: