• If I run this

    declare @salesperson table(pid int,name nvarchar(10),sales int)

    insert into @salesperson

    values (1,'gary',0),(2,'sam',0)

    declare @salesHeader table(id int,pid int, sdate date,amt int)

    insert into @salesHeader

    values

    (2,1,'2014-04-21',130),(1,1,'2014-05-29',100), (4,2,'2014-04-14 ',40), (3,2,'2014-05-29 ',90)

    select * from @salesHeader /*to audit */

    select * from @salesPerson /* to audit */

    UPDATE A

    SET Sales = Sales +

    (SELECT SUM(so.amt)

    FROM @salesHeader AS so

    WHERE

    --so.sDate = (SELECT MAX(sDate)

    -- FROM @salesHeader AS so2

    -- WHERE so2.pid = so.pid)

    --AND

    A.pid = so.pid

    GROUP BY so.pid )

    from @salesperson as A

    select * from @salesperson

    It gives the results I would expect, else it just updates the YTD sales with whatever it is the sales person sold ($) on their last day of sales (which would not make logical sense to me given the name of the column in the original query ... SalesYTD). The most inner query finds the last day a person made a sale and sums all the sales orders he/she sold on that day. So if this was not run every day ( I assume it is writing to a daily snapshot table) and skips a day, this bug gets introduced.

    ----------------------------------------------------