473,386 Members | 1,748 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,386 software developers and data experts.

update with a one to many

Hi,

I am trying to do the following

update table A with the latest record from table B.

TABLE A has
PID (primary key)
ID
YEAR

TABLE B is the same as above. There is a one to many relationship from A to B based on the ID.

I ran the following update and it worked but it took the first record for each ID in TABLE B and updated that to TABLE A. Instead, i want to be able to update TABLE A only with the most recent record from B

UPDATE A
SET A.YR = B.YR
FROM A,B
WHERE A.ID=B.ID

I also tried creating a temp table with the information from table B and then ordering that table and then running the update, but that still did not update the latest record?

Thanks
Chris
Jan 30 '08 #1
3 5332
deepuv04
227 Expert 100+
Hi,

I am trying to do the following

update table A with the latest record from table B.

TABLE A has
PID (primary key)
ID
YEAR

TABLE B is the same as above. There is a one to many relationship from A to B based on the ID.

I ran the following update and it worked but it took the first record for each ID in TABLE B and updated that to TABLE A. Instead, i want to be able to update TABLE A only with the most recent record from B

UPDATE A
SET A.YR = B.YR
FROM A,B
WHERE A.ID=B.ID

I also tried creating a temp table with the information from table B and then ordering that table and then running the update, but that still did not update the latest record?

Thanks
Chris

i didnt get you exactly what you want....

try the following query probably will help you....( from what i understand from the above )


UPDATE A
SET A.YR = b.yr
FROM A,B
WHERE A.ID=B.ID
and b.id = ( select top 1 id from b order by id desc)


or

UPDATE A
SET A.YR = b.yr
FROM A,B
WHERE A.ID=B.ID
and a.id = ( select top 1 id from b order by id desc)


thanks
Jan 30 '08 #2
debasisdas
8,127 Expert 4TB
In a temporary table insert the new record entry of table B.

Delete the old records from the temp table.

Update table A from temp table .
Jan 31 '08 #3
Thank you deepuv04.

That is what i was looking for.
Feb 18 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
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...
5
by: rdraider | last post by:
Hi all, I'm looking for a way to re-number inventory items. The items exist in 50+ tables, hundreds of fields and there are several thousand items. Maybe one table could hold the list of old &...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
4
by: David Colliver | last post by:
Hi all, I am having a slight problem that hopefully, someone can help me fix. I have a form on a page. Many items on the form have validation controls attached. Also on this form are...
29
by: Geoff Jones | last post by:
Hi All I hope you'll forgive me for posting this here (I've also posted to ado site but with no response so far) as I'm urgently after a solution. Can anybody help me? I'm updating a table on...
8
by: puzzlecracker | last post by:
Say you have a class: stuct Updater{ int _v1; int _v2 int _v3; update(int v1, int v2, int v2); bool isBad(int val); };
8
geolemon
by: geolemon | last post by:
I'm trying to perform an update, and I can't avoid this error - I've tried this query what must be 5 different very fundamentally different ways now. Arg. I used to be a DBA in large DB2 and SQL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.