Need to understand how queries and this sample query is processed.

  • I took the Adventure works example below and created my own sample to see if I can understand how this works. My example does not work. I really want to understand how this statement works. Can someone break it down in terms of what is the input to each part of the statement?

    USE AdventureWorks2012;

    GO

    UPDATE Sales.SalesPerson

    SET SalesYTD = SalesYTD +

    (SELECT SUM(so.SubTotal)

    FROM Sales.SalesOrderHeader AS so

    WHERE so.OrderDate = (SELECT MAX(OrderDate)

    FROM Sales.SalesOrderHeader AS so2

    WHERE so2.SalesPersonID = so.SalesPersonID)

    AND Sales.SalesPerson.BusinessEntityID = so.SalesPersonID

    GROUP BY so.SalesPersonID);

    GO

    My sample:

    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

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

    UPDATE @salesperson

    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 pid = so.pid

    GROUP BY so.pid );

    select * from @salesperson

  • Since you have more than one date value, that's returning multiple rows in a place that can only have one. Instead of using MAX date, use TOP 1 with an order by. That will return just the first matching date row.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • This was a head scratcher. I think that because you used a table variable in your example, you need to use an alias so that sql can make the link back to the table to be updated i.e. we need to mimic the line

    'AND Sales.SalesPerson.BusinessEntityID = so.SalesPersonID' whereas your line was 'AND pid = so.pid' which sent the query to find multiple values in the subquery like Grant says. I hope that makes sense.

    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

  • It doesn't seem to update the results the right way, at least from my understanding..

    I get results as

    pidnamesales

    1gary 100 --instead of 230

    2sam 90 -- instead of 130

    Did I understand this correctly? Or what is it that the query is trying to do?

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

  • 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.

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

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply