SQL Dojo Bêta
Technique

Cumul

Additionner une mesure de la première ligne jusqu'à la ligne courante.
SELECT month, ca, SUM(ca) OVER (ORDER BY month) AS running_total
FROM monthly_sales;

Ajoutez PARTITION BY pour repartir de zéro dans chaque groupe (chaque année, chaque client).

WITH monthly AS (
  SELECT CAST(date_trunc('month', o.ordered_at) AS DATE) AS month,
         SUM(l.quantity * l.unit_price) AS revenue
  FROM orders o JOIN order_lines l ON l.order_id = o.order_id
  WHERE o.status <> 'cancelled' AND o.ordered_at < DATE '2024-05-01'
  GROUP BY 1
)
SELECT month, revenue, SUM(revenue) OVER (ORDER BY month) AS running_total
FROM monthly
ORDER BY month;
monthrevenuerunning_total
2024-01-015874.705874.70
2024-02-015611.3011486.00
2024-03-017261.7018747.70
2024-04-015768.5024516.20
L’essayer en Randori