Forum

Posted by sonalee, May 15, 2025, 12:52 a.m.

69. Number of happy customers

I'm trying to write a query that returns the number of "happy customers" — defined as customers who made at least one rental on each day of any 2 consecutive days within the date range May 24 to May 31 (inclusive).

šŸ“Š Expected Result:
The expected count is 159.

šŸ’» My Environment:

  • Database: PostgreSQL

    🧩 What I've Tried So Far:

    • I used LEAD() to compare each customer's rental date with the next one.

    • Filtered where the next date is exactly 1 day ahead.

    • Counted customers with at least one such occurrence.

    ā— Issue:
    This returns 123, but the expected answer is 159. I'm unsure where the logic fails or whether I'm missing edge cases like multiple rentals in a day or overlapping rental dates.


    šŸ™ Would appreciate help with:

    • Identifying if my logic is flawed for how “consecutive rentals” are calculated.

    • Suggestions for improving the query or a different approach entirely.

    Thanks in advance!

This question is still open. Write your answer.