🌱 Calculate Moving Average in PostgreSQL
SELECT a.order_date,a.sale,
AVG(a.sale)
OVER(ORDER BY a.order_date ROWS BETWEEN 4 PRECEDING AND CURRENT ROW)
AS avg_sales
FROM sales a ;
SELECT a.order_date,a.sale,
AVG(a.sale)
OVER(ORDER BY a.order_date ROWS BETWEEN 4 PRECEDING AND CURRENT ROW)
AS avg_sales
FROM sales a ;