SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sanjay5219
Posting Yak Master

236 Posts

Posted - 10/30/2013 :  14:58:00  Show Profile  Reply with Quote
Hi All,

I have two tables T1 and T2
T1 is having User : Visiting Date
T2 IS having User : Last Visited Date

Now I have to update T2 with last visited date based on T1 Entry.

Please suggest the query

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 10/30/2013 :  15:03:27  Show Profile  Reply with Quote
you mean this?

update t2 set
       t2.lastVisitDate = d.MaxVD
from   (
       select user, max(VisitDate) maxVD
       from   T1
       group by user
       ) d
inner join T2
       on t2.user = t1.user
where  t2.lastVisitDate != d.MaxVD


If you also need to insert new Users to T2 then you may want to look at a MERGE statement.

Be One with the Optimizer
TG
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000