Easy SQL Challenge: Using Windows Function

365 Days of Coding Challenge: Day 72

In order to save my lazy ass, I solved an easy challenge instead of the medium challenge that I was supposed to continue from yesterday. I solved an easy sql challenge but to me it seemed as though it was a medium challenge cause I have never done cumulative sums before.

With the help of Stackoverflow and some experimenting, I was able to arrive at the solution.

SELECT date, customer_id, daily_spend,
SUM(daily_spend) OVER(PARTITION BY customer_id ORDER BY date) AS cumulative_spend
FROM
(SELECT DATE(payment_ts) AS date, customer_id, SUM(amount) AS daily_spend
FROM payment
WHERE customer_id IN (1,2,3)
GROUP BY date, customer_id) AS k;

Leave a comment