Running total

Cumulative revenue per day with SUM() OVER.

sql intermediate #window-functions#reports
プレイグラウンドで開く
sql-running-total.sql
SELECT day,
       revenue,
       SUM(revenue) OVER (ORDER BY day) AS running_total
FROM (
    SELECT created_at::date AS day, SUM(total) AS revenue
    FROM orders
    GROUP BY 1
) daily
ORDER BY day;