Forum

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!

Answers

Hi Thomas,

First of all, thank you for sharing your solution here, please feel free to share your solutions with the community, as we are all learning from each other.

I like your solution, it is clean and easy to understand. 

The only comment is that your solution is based on MySQL, and doesn't work with Postgres, might be helpful to mark your solution's database engine as of MySQL.

 

Thanks,

Leon

SQLPad user avatar

Leon (949)

May 10, 2022, 8:38 a.m.