Posted by Yang, Sept. 12, 2022, 9:21 a.m.
Question 118 Fill in 0s for dates with no booking
Hello, I understand the tricky part of this question is there are days with no bookings so 0s need to be filled in by having a full date table LEFT JOINED with the transactional table. However, I don't understand why my code won't work.
Could someone take a look?
SELECT
d.date
,CASE WHEN COUNT(DISTINCT booking_id) IS NULL THEN 0 ELSE COUNT(DISTINCT booking_id) END AS booking
FROM dates d
LEFT JOIN bookings b on d.date = b.date
LEFT JOIN listings l on b.listing_id = l.listing_id
WHERE
d.date BETWEEN '2021-07-01' AND '2021-07-31'
AND l.country = 'US'
GROUP BY 1