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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update Query

Author  Topic 

sanjay5219
Posting Yak Master

240 Posts

Posted - 2013-10-30 : 14:58:00
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
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-10-30 : 15:03:27
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
   

- Advertisement -