| Author |
Topic |
|
Crespo24
Village Idiot
144 Posts |
Posted - 2003-01-08 : 05:27:53
|
OK.... I will try and explain this as best as I could.I have a table consisting of two columns, member_key and a date. Each record is unique combination of member_key and a date.I also have a second table which hold multiple records for each member_key. For example,Table1Member_key | Date11000 | 01/01/2001Table2Member_key | Contribution_date1000 | 01/01/19981000 | 01/01/19781000 | 01/01/19931000 | 01/01/2002What I need to do is update the second table and replace the min(contribution_date) with the date that is in table1. So in other words, replace 01/01/1978 with 01/01/2001 which is the date in Table1.Any ideas?Thanks. |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2003-01-08 : 05:35:37
|
Hi CrespoOK, basically you need to use this sort of a technique but with a subquery.Something like this (untested)UPDATE Table2SET contribution_date = date1FROMTable2INNER JOIN Table1 ON Table1.Member_Key = Table2.Member_KeyWHEREcontibution_date = (SELECT MIN(contribution_date) FROM table2 t2WHERE member_key = Table2.member_key) Give that a try.DamianEdited by - merkin on 01/08/2003 05:36:03 |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2003-01-08 : 05:38:11
|
| you need a correlated UPDATE statement....a forum search on "update, from ,where" all-words option trns up loads of examples....the following link is onehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=22543have a look at the structure of the UPDATE statement in ValterBorges reply....ignore the CASE part...not relevent for you i believe....you may have to play around with the "where" clause. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-01-08 : 06:07:00
|
| update Table2set Contribution_date = Table1.Date1where Table1.Member_key = Table2.Member_keyand Contribution_date = (select min(Contribution_date) from Table2 t2 where t2.Member_key = Table2.Member_key)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Crespo24
Village Idiot
144 Posts |
Posted - 2003-01-08 : 06:07:19
|
Thanks all... it works  |
 |
|
|
stevep
Starting Member
17 Posts |
Posted - 2003-01-12 : 21:22:33
|
| Can I add my own complication to a similar query.I've a table listing project deliverables (id, name, etc) and a tablelisting versions of those deliverables (description, date submitted, etc).Using the a sub-query suggested in the above discussion I've got a query that gives me the most recent version of each deliverable for a given project:SELECT deliverables.deliverableID, deliverables.deliverableName, deliverableVersions.dvVersion, deliverableVersions.dvDescription, deliverableVersions.dvSubmittedDateFROM deliverables LEFT JOIN deliverableVersions ON deliverables.deliverableID = deliverableVersions.dvDeliverableWHERE (((deliverableVersions.dvSubmittedDate)=(SELECT MAX(dvSubmittedDate) FROM deliverableVersions DV2 WHERE dv2.dvDeliverable = deliverables.deliverableID)) AND ((deliverables.deliverableProject)=10));Now the bit that has be stumped is how to include in the query results the names of deliverables that do not have a version recorded yet. However I define the join between deliverables and versions I cannot seem to retrieve details of a deliverable that has no versions.Ta for help.Steve |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-01-13 : 07:35:10
|
| Never use a WHERE on the outer table of an outer join -- it turns your outer join into an inner join.Example:SELECT blah blahFROM ALEFT OUTER JOIN BON A.ID = B.IDWHERE B.Value > 0is the same asSELECT blah blahFROM AINNER JOIN BON A.ID = B.IDWHERE B.Value > 0Because the WHERE eliminates Nulls.The solution is eitherSELECT Blah blahFROM ALEFT OUTER JOIN BON A.ID = B.ID AND B.Value > 0 orSELECT blah blahFROM ALEFT OUTER JOIN(SELECT * FROM B WHERE Value > 0) BON A.ID = B.ID Does that help? Try using the above as a guideline to rewrite your SQL. If you are still stuck, let me know. But I find deriving the answer yourself is usually more useful because then you really know what's going on.- Jeff |
 |
|
|
stevep
Starting Member
17 Posts |
Posted - 2003-01-13 : 19:06:37
|
| Jeff,Ta muchly, both for the suggestion and the explanation of why my version did not work. Have managed to get half of it to work:SELECT B.deliverableID, B.deliverableName, A.dvVersion, A.dvDescription, A.dvSubmittedDateFROM deliverableVersions AS A RIGHT JOIN [SELECT * FROM deliverables WHERE deliverableProject = 10]. AS B ON A.dvDeliverable = B.deliverableIDNow I'm guessing to include the part of the original WHERE that included only the most recent version I need to change that into an a single SQL statement that brings back the required table of versions. Am now working that bit of it out (slowly).Thanks again.Steve |
 |
|
|
|