473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

UPDATE JOIN TOP 1

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
Nov 28 '06 #1
4 18907
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)

"Darren Woodbrey" <it@hpfairfield.comwrote in message
news:12*************@corp.supernews.com...
>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

Nov 28 '06 #2
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"
<it@hpfairfield.comwrote:
>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)

"Darren Woodbrey" <it@hpfairfield.comwrote in message
news:12*************@corp.supernews.com...
>>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
Nov 28 '06 #3
That is it. Thanks so much for your help!
"Roy Harvey" <ro********@snet.netwrote in message
news:uv********************************@4ax.com...
>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"
<it@hpfairfield.comwrote:
>>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)

"Darren Woodbrey" <it@hpfairfield.comwrote in message
news:12*************@corp.supernews.com...
>>>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

Nov 28 '06 #4
Roy Harvey (ro********@snet.net) writes:
The other is that the subquery's FROM clause should NOT include the
table being updated.
And then people keep yelling on the FROM syntax of being dangerous!
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Nov 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Narine | last post by:
Hi All, I need to write one complicated update statement and I'm looking at maybe finding a simpler way to do it. I have 2 tables: 1.Photo Table PhotoID FileName 1 111.jpg
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
2
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
9
by: Vorpal | last post by:
Here is a small sample of data from a table of about 500 rows (Using MSSqlserver 2000) EntryTime Speed Gross Net ------------------ ----- ----- 21:09:13.310 0 0 0 21:09:19.370 9000 ...
4
by: 001 | last post by:
Hello, The select statement needs only 1 second to complete the query. But the update statement spends 30 minutes. Why? SELECT STATEMENT: declare @IDate smalldatetime select @IDate=col001...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
4
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET...
2
by: Sim Zacks | last post by:
The following query updated all the rows in the AssembliesBatch table, not just where batchID=5. There are 2 rows in the AssembliesBatch table with batch ID of 5 and I wanted to update both of...
0
by: svgeorge | last post by:
I want to update several tables using one stored procedure. How can i do this I mean the syntax.etc. declaration etc. I know the basic syntax as below CREATE PROCEDURE <Procedure_Name, sysname,...
3
by: Michel Esber | last post by:
Hi all, DB2 V8 LUW FP 15 There is a table T (ID varchar (24), ABC timestamp). ID is PK. Our application needs to frequently update T with a new value for ABC. update T set ABC=? where ID...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.