SQLRepPath
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
00/60 solved

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…