Forum

Posted by gurpreet, Nov. 29, 2021, 4:12 p.m.

SQL#119

Hi,
I am getting the expected results from following query, but it is not being accepted.
With countries_by_dates as (
SELECT c.date,
b.country,
count(a.booking_id) as bookings
FROM dates c
left join bookings a
on a.date = c.date
left join listings b
on a.listing_id = b.listing_id
where a.date >= '2021-08-01' and a.date <= '2021-08-31'
GROUP BY c.date, b.country
),

rankings as (
SELECT date,
country,
bookings,
row_number() over (partition by date order by bookings desc) as ranking
FROM countries_by_dates
)

SELECT *
FROM rankings
WHERE ranking <=3

Answers

Hi Gurpreet,
It turns out there are ties for #3 countries on two dates, and the DB engine randomly assigned a country with the same amount of bookings.
2021-08-15 JP 68 3 2021-08-15 MX 68 3 FALSE
2021-08-17 JP 27 3 2021-08-17 UK 27 3 FALSE
I've updated the question and now only ask the top 2 countries, your solution will pass the OJ now.
Thanks,
Leon
SQLPad user avatar

Leon (949)

Nov. 30, 2021, 9:17 a.m.