Posted by Mansi, Nov. 26, 2021, 12:31 p.m.
Regarding question no 122
I was wondering why I can't put date filter in first cte(nth_bookings ) and filter the data before. something like this instead of putting in wow_change cte
WITH nth_bookings AS (
SELECT D.date, booking_id, country, ROW_NUMBER() OVER (PARTITION BY country, B.listing_id ORDER BY D.date) AS nth_booking
FROM dates D
LEFT JOIN bookings B
ON B.date = D.date
LEFT JOIN listings L
ON L.listing_id = B.listing_id
WHERE b.date >= '2021-08-01'
AND b.date < '2021-09-01'
),
first_booking AS (
SELECT * FROM nth_bookings
WHERE nth_booking = 1
),
daily_first_booking AS (
SELECT date, country, COUNT(*) AS first_bookings
FROM first_booking
GROUP BY date, country
),
wow_change AS (
SELECT date, country, (first_bookings - LAG(first_bookings, 7) OVER()) * 100.0/ (LAG(first_bookings, 7) OVER()) AS wow_change
FROM daily_first_booking
)
SELECT date, country, ranking
FROM (
SELECT date, country, ROW_NUMBER() OVER(PARTITION BY date ORDER BY wow_change DESC) ranking
FROM wow_change
) X
WHERE ranking <= 3
ORDER BY date, ranking;
WITH nth_bookings AS (
SELECT D.date, booking_id, country, ROW_NUMBER() OVER (PARTITION BY country, B.listing_id ORDER BY D.date) AS nth_booking
FROM dates D
LEFT JOIN bookings B
ON B.date = D.date
LEFT JOIN listings L
ON L.listing_id = B.listing_id
WHERE b.date >= '2021-08-01'
AND b.date < '2021-09-01'
),
first_booking AS (
SELECT * FROM nth_bookings
WHERE nth_booking = 1
),
daily_first_booking AS (
SELECT date, country, COUNT(*) AS first_bookings
FROM first_booking
GROUP BY date, country
),
wow_change AS (
SELECT date, country, (first_bookings - LAG(first_bookings, 7) OVER()) * 100.0/ (LAG(first_bookings, 7) OVER()) AS wow_change
FROM daily_first_booking
)
SELECT date, country, ranking
FROM (
SELECT date, country, ROW_NUMBER() OVER(PARTITION BY date ORDER BY wow_change DESC) ranking
FROM wow_change
) X
WHERE ranking <= 3
ORDER BY date, ranking;