round 3

Python/pandas or datawarehouse/ dataModeling



Round 2

rolling window as per actual month (i.e if current month is apr then sum for all feb,mar and apr)

SELECT
    SaleDate,
    SalesAmount,
    SUM(SalesAmount) OVER (
        ORDER BY SaleDate
        RANGE BETWEEN INTERVAL '3 MONTH' PRECEDING AND CURRENT ROW
    ) AS RunningTotalLast3Months
FROM
    DailySales
ORDER BY
    SaleDate;

it will give the sum of previous last 3 rows including current

SELECT
    your_date_column,
    your_value_column,
    SUM(COALESCE(your_value_column, 0)) OVER (
        ORDER BY your_date_column
        ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
    ) AS running_total_3_month_lag
FROM
    your_table;