• Oh, inherited mess... my favorite! Here's a quick example of using a window function.

    SELECT pmts.ID

    , pmts.Amount

    , pmts.[Status]

    , SUM([Amount]) OVER (ORDER BY ID

    ROWS BETWEEN UNBOUNDED PRECEDING

    AND CURRENT ROW) AS RunAmt

    FROM

    (SELECT 1 AS ID

    , 15 AS Amount

    , 'Paid' As [Status]

    UNION ALL

    SELECT 2, 3, 'Paid'

    UNION ALL SELECT 3,10,'Awaiting'

    UNION ALL

    SELECT 4, 12,'Awaiting') pmts;

    If you wanted to do a GROUP BY in your running total, it would be like this (note the PARTITION BY):

    SELECT pmts.OwnerID

    , pmts.ID

    , pmts.Amount

    , pmts.[Status]

    , SUM([Amount]) OVER (PARTITION BY OwnerID ORDER BY ID

    ROWS BETWEEN UNBOUNDED PRECEDING

    AND CURRENT ROW) AS RunAmt

    FROM

    (SELECT 'a' AS OwnerID

    , 1 AS ID

    , 15 AS Amount

    , 'Paid' As [Status]

    UNION ALL

    SELECT 'a',2, 3, 'Paid'

    UNION ALL SELECT 'a',3,10,'Awaiting'

    UNION ALL

    SELECT 'b',4, 12,'Awaiting') pmts;