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 returns123, but the expected answer is159. 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!
-