Problem Statement
How would you compute a running total that only counts 'PAID' invoices while still showing all invoice rows?
Explanation
Use a conditional expression inside the window aggregate. Wrap the amount in CASE so only qualifying rows contribute to the running sum. The window still spans all rows in order, so non-paid rows appear with the same running total carry-over.
This pattern is efficient because the table is scanned once and the condition is applied row by row. It avoids extra joins and keeps the business rule close to the metric.
Code Solution
SolutionRead Only
SELECT invoice_id, status, amount,
SUM(CASE WHEN status='PAID' THEN amount ELSE 0 END)
OVER (ORDER BY issued_at ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS paid_running
FROM invoices
ORDER BY issued_at;