Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
groads2
groads2
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 63
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


Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17576 Visits: 32260
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 Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
DouglasH
DouglasH
SSC-Enthusiastic
SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)

Group: General Forum Members
Points: 125 Visits: 1469
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





MMartin1
MMartin1
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1088 Visits: 2014
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?

----------------------------------------------------
How to post forum questions to get the best help
MMartin1
MMartin1
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1088 Visits: 2014
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.

----------------------------------------------------
How to post forum questions to get the best help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search