• Finally dug up Itzik's book on T-SQL 2008...

    -- from p.458 "Inside SQL Server 2008 T-SQL Querying" By Itzik Ben-Gan

    SELECT 01.empID,

    CONVERT(VARCHAR(7), o1.ordMonth, 121) AS toMonth,

    o1.qty AS qtyThisMonth,

    SUM(o2.qty) AS totalQty,

    CAST(AVG(1 * o2.qty) AS NUMERIC(12,2)) AS avgQty

    FROM dbo.EmpOrders AS o1

    JOIN dbo.EmpOrders o2

    ON o2.empID = 01.empID

    AND (02.ordmonth > DATEADD(month, -3, o1.ordMonth)

    AND o2.ordMonth <= o1.ordMonth)

    GROUP BY o1.empID, o1.ordMonth, o1.qty

    ORDER BY o1.empID, o1.ordMonth;

    Tweaking IBG's solution to your structure...

    SELECT x1.username

    , x1.move_in_date

    , SUM(x2.cnt_lead_id) as totalqty

    , CAST(AVG(1 * x2.cnt_lead_id) aS NUMERIC(12,2)) AS avgQty

    FROM dbo.countHistory x1

    JOIN dbo.countHistory x2

    ON x2.username = x1.username

    AND x2.move_in_date<=x1.move_in_date

    GROUP BY x1.username, x1.move_in_date, x1.cnt_lead_id

    ORDER BY x1.username, x1.move_in_date;

    I think that's right... but anything involving my brain and T-SQL at 4AM could hardly be called "thinking".