Posted by bharath, June 9, 2024, 9:39 a.m.
[#69] : Number of happy customers [POSTGRESQL]
Number of Happy Customers Ans:
WITH CTE AS (
SELECT
customer_id,
DATE(rental_ts) AS curr_date,
LEAD(rental_ts, 1) OVER (PARTITION BY customer_id ORDER BY rental_ts) AS next_rental_ts,
DATE_PART('day', LEAD(rental_ts, 1) OVER (PARTITION BY customer_id ORDER BY rental_ts) - DATE(rental_ts)) AS days_between_rentals
FROM rental
WHERE
EXTRACT(MONTH FROM rental_ts) = 5
AND EXTRACT(DAY FROM rental_ts) BETWEEN 24 AND 31 )
SELECT
COUNT(DISTINCT customer_id)
FROM CTE
WHERE days_between_rentals = 1;