Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need to understand how queries and this sample query is processed. Expand / Collapse
Author
Message
Posted Thursday, May 29, 2014 8:07 PM


Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 10, 2014 8:09 AM
Points: 9, Visits: 51
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

Post #1575928
Posted Friday, May 30, 2014 3:11 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 8:41 AM
Points: 15,516, Visits: 27,892
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
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1575984
Posted Friday, May 30, 2014 7:23 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 6:15 AM
Points: 63, Visits: 345
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




Post #1576064
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse