all groups > sql server (alternate) > november 2006 >
You're in the

sql server (alternate)

group:

UPDATE JOIN TOP 1


UPDATE JOIN TOP 1 Darren Woodbrey
11/28/2006 11:06:11 AM
sql server (alternate):
I am trying to update 1 table with the top records from another table for
each record in the first table

UPDATE HPFSLOWMOVING
SET TOP 1 LASTRCTDATE = (SELECT DOCDATE FROM IV30300 INNER JOIN
HPFSLOWMOVING ON HPFSLOWMOVING.ITEMNMBR = IV30300.ITEMNMBR AND
HPFSLOWMOVING.LOCNCODE = IV30300.LOCNCODE WHERE DOCTYPE = 4)

This updates all records with the same lastrctdate. I need to update each
records with the top lastrctdate where the itemnmbr and locncode equals.
Thanks for any help you can provide!


Darren

Re: UPDATE JOIN TOP 1 Darren Woodbrey
11/28/2006 11:13:55 AM
My query below is wrong. This is what I have so far:

UPDATE HPFSLOWMOVING
SET LASTRCTDATE = (SELECT TOP 1 DOCDATE FROM IV30300 INNER JOIN
HPFSLOWMOVING ON HPFSLOWMOVING.ITEMNMBR = IV30300.ITEMNMBR AND
HPFSLOWMOVING.LOCNCODE = IV30300.LOCNCODE WHERE DOCTYPE = 4)



[quoted text, click to view]

Re: UPDATE JOIN TOP 1 Darren Woodbrey
11/28/2006 11:49:41 AM
That is it. Thanks so much for your help!


[quoted text, click to view]

Re: UPDATE JOIN TOP 1 Roy Harvey
11/28/2006 4:31:53 PM
I see two problems. One is that the subquery uses TOP without an
ORDER BY. Without ORDER BY, TOP simply returns any one row.

The other is that the subquery's FROM clause should NOT include the
table being updated.

Try something along the lines of this:

UPDATE HPFSLOWMOVING
SET LASTRCTDATE =
(SELECT TOP 1 DOCDATE
FROM IV30300
WHERE HPFSLOWMOVING.ITEMNMBR = IV30300.ITEMNMBR
AND HPFSLOWMOVING.LOCNCODE = IV30300.LOCNCODE
AND DOCTYPE = 4
ORDER BY ??????)

Roy Harvey
Beacon Falls, CT

On Tue, 28 Nov 2006 11:13:55 -0500, "Darren Woodbrey"
[quoted text, click to view]
Re: UPDATE JOIN TOP 1 Erland Sommarskog
11/28/2006 10:39:45 PM
Roy Harvey (roy_harvey@snet.net) writes:
[quoted text, click to view]

And then people keep yelling on the FROM syntax of being dangerous!
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button