• A much more efficient version of this takes about 1/6 of the processing:

    SELECT G.OrderDate, G.ProductKey, QtyOrdered = SUM(F.QtyOrdered), G.MTD

    FROM

    (

    SELECT

    ProductKey

    , OrderDate

    , MTD= (SELECT sum(QtyOrdered) FROM #t WHERE OrderDate <= a.OrderDate AND ProductKey = a.ProductKey)

    FROM #t A

    GROUP BY ProductKey, OrderDate

    ) G

    JOIN #t F

    ON G.ProductKey = F.ProductKey AND G.OrderDate = F.OrderDate

    GROUP BY G.ProductKey,G.OrderDate, G.MTD

    ORDER BY G.ProductKey,G.OrderDate;