Posted by Thomas, May 9, 2022, 7:51 p.m.
Question 69 - Alternate Solution [MySQL]
I am not sure if this is similar to Leetcode where commenters offer alternative solution in the event they are more helpful to everyone. If this is not allowed, feel free to remove!
Here's an alternative answer to #69 with only 1 CTE as opposed to 2 and the addition of lead().
with rentals as (
select customer_id, date(rental_ts) as curr,
lag(rental_ts, 1) over (partition by customer_id order by rental_ts asc) as rental_prev
from rental
where date(rental_ts) between '2020-05-24' and '2020-05-31'
)
select count(distinct customer_id)
from rentals
where abs(datediff(curr, rental_prev)) = 1
Feedback always appreciated!