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: Yesterday @ 4:57 PM
Points: 15,661, Visits: 28,047
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: Monday, August 18, 2014 10:12 AM
Points: 63, Visits: 365
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
Posted Thursday, July 24, 2014 5:54 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 3:05 PM
Points: 355, Visits: 870
It doesn't seem to update the results the right way, at least from my understanding..
I get results as

pid name sales
1 gary 100 --instead of 230
2 sam 90 -- instead of 130

Did I understand this correctly? Or what is it that the query is trying to do?
Post #1596049
Posted Thursday, July 24, 2014 6:04 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 3:05 PM
Points: 355, Visits: 870
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.
Post #1596050
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse