Interview Drills · SUM() OVER · running totals
22. Running revenue total
Concept: SUM() OVER · running totalsshow
Putting ORDER BY inside OVER() turns an aggregate into a running calculation: SUM(x) OVER (ORDER BY day) is the total of x from the first row up to the current one. Every row is kept.
Example (different data):
SELECT day, signups, SUM(signups) OVER (ORDER BY day) AS cumulative FROM daily_signups;
Your task
Fintech classic: the treasury dashboard needs cumulative completed revenue through July, one row per order.
For completed orders, return order_date, amount and running_total (cumulative sum of amount by order_date, rounded to 2 decimals), oldest first.
Starting Postgres in your browser…
Checking your access…