Time-Series Analysis with Lag and Lead

Time-series analysis uses functions like LAG and LEAD to compare rows in sequence.


SELECT 
    customer_id,
    purchase_date,
    amount,
    LAG(amount) OVER (PARTITION BY customer_id ORDER BY purchase_date) AS previous_purchase
FROM 
    sales;