update table from another table join

Hi experts
i have 2 tables i need to update the first table depend on the last record from the second table

Table 1
Reference    Date                                   Complete Date
---------------------------------------------------------------------------
42275            24-Jan-2011 09:33 AM        NULL      
338402            27-Mar-2010 08:03 AM        NULL      

Table 2
Id             Reference   Status                        Date                                  Complete Date
-----------------------------------------------------------------------------------------------------------------------------
26710      42275           Task Assignment      24-Jan-2011 09:53 AM         03-Feb-2011 08:00 AM            
25365      42275           Task Approval            24-Jan-2011 09:33 AM         24-Jan-2011 09:53 AM            
25361      42275           Use this form          24-Jan-2011 09:19 AM         24-Jan-2011 09:33 AM
2831      338402           Task Approval              27-Mar-2010 08:03 AM  28-Mar-2010 08:03 AM
2768      338402           Use this form            27-Mar-2010 07:33 AM  27-Mar-2010 07:55 AM                        

i need to update [Table 1].[Complete Date] with last record from [Table 2].[Complete Date] depend on Maximum [Table 1].[Id] and [Table 1].[Reference] = [Table 2].[Reference]

can you please help me in writing the Query
LVL 1
AZZA-KHAMEESAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
this article should help:
http://www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html

in short:
update t1
   set t1.[Complete Date] = ( select t2.[Complete Date]  from [table 2] t2
                                   where t2.id = ( select max(x.id) from [table 2] x where x.[Reference] = t1.[Reference] )
                                         and t2.Reference = t1.Reference
                      )
  from [table 1] t1

Open in new window

AZZA-KHAMEESAuthor Commented:
thank you for the reply, but i am getting the following error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
that error would mean you may have more that 1 for with the same ID and Reference ...

does it go away with this
update t1
   set t1.[Complete Date] = ( select max(t2.[Complete Date] ) from [table 2] t2
                                   where t2.id = ( select max(x.id) from [table 2] x where x.[Reference] = t1.[Reference] )
                                         and t2.Reference = t1.Reference
                      )
  from [table 1] t1
                                          

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.