Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

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

Posting Yak Master

240 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

Flowing Fount of Yak Knowledge

6065 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
Go to Top of Page
  Previous Topic Topic Next 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.02 seconds. Powered By: Snitz Forums 2000