Easy SQL Challenge: Using Windows Function to Find Cumulative Sum

365 Days of Daily Coding: Day 72

My lazy a$$ did it again. I couldn’t be bothered to solve past challenges that I was unable to solve. Since I wanted to save myself, I solved an easy challenge again which was pretty similar to yesterday’s challenge. Tomorrow I am going to make it for sure.

One thing I have started doing since last year was create a sort of daily log/to-do-list every morning at the start of the day. Sometimes I don’t and I just drift in abyss. Having an intention filled day is important for me to feel productive and sleep without feeling regret. I will solve atleast 2 challenges tomorrow.

Easy Challenge:

SELECT date, customer_id, daily_rental,
SUM(daily_rental) OVER (PARTITION BY customer_id ORDER BY date)
AS cumulative_rentals
FROM
(SELECT DATE(rental_ts) AS date, customer_id, 
COUNT(rental_id) AS daily_rental
FROM rental
WHERE customer_id IN (3,4,5)
GROUP BY date, customer_id
ORDER BY customer_id, date) AS k;

Leave a comment