Author |
Topic |
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-12-22 : 06:15:07
|
Hi all, I am having two tables session and log I need to update the log table logout field when the session table logout field get modifiedThe two tables having only one common field called User_id sample datas are here.session table:Session_Id session_key User_Id Login_dete Log_out Is_Active IP1760,a40x5q45wqprlryjkxr3mazm,137,2010-12-22 14:50:23,2010-12-22 15:50:04,N,127.0.0.11761,a40x5q45wqprlryjkxr3mazm,137,2010-12-22 15:04:15,2010-12-22 15:50:04,N,127.0.0.11762,a40x5q45wqprlryjkxr3mazm,137,2010-12-22 15:28:48,2010-12-22 15:50:04,N,127.0.0.1Log table:User_Id login_date user_role Is-Active log_out_time account_reset 137,2010-12-22 14:50:23,NULL,Y,NULL,NULL137,2010-12-22 15:04:14,NULL,Y,1,NULL,NULL137,2010-12-22 15:28:14,NULL,Y,1,NULL,NULL |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-12-22 : 06:24:03
|
update lset log_out=s.log_out from log_table as l inner join session_table as son l.user_id=s.user_idMadhivananFailing to plan is Planning to fail |
 |
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-12-22 : 06:48:40
|
Thank you sir for your reply ..I tried this but it affects all the rows of user_id ..But i need the the last row of that user must change.. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-12-23 : 03:00:44
|
What did you mean by last row?MadhivananFailing to plan is Planning to fail |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-12-23 : 04:22:21
|
This ?declare @session table(Session_Id int, User_Id int,Log_out datetime)insert @session select 1760,137,'2010-12-22 15:50:04' unionselect 1761,137,'2010-12-22 15:50:04'unionselect 1762,137,'2010-12-22 15:50:04'unionselect 1762,138,'2010-12-22 15:50:14'unionselect 1763,138,'2010-12-22 15:50:14'declare @Log table(user_id int, Log_in datetime,Log_out datetime)insert @Log select 137,'2010-12-22 14:50:23',NULL unionselect 137,'2010-12-22 15:04:14',NULL unionselect 137,'2010-12-22 15:28:14',NULL unionselect 138,'2010-12-22 15:28:24',NULL unionselect 138,'2010-12-22 15:28:34',NULLselect * from @Logupdate L set L.Log_out=L1.log_outfrom @Log Linner join(select max(Session_Id)Session_Id, user_id, min(log_out)log_out, min(Log_in)Log_infrom @session as scross apply(select top 1 Log_in from @Log l where s.user_id=l.user_id order by l.Log_in desc)L2group by user_id)L1 on L.Log_in=L1.Log_in and L.user_id=L1.User_Idselect * from @Log PBUH |
 |
|
|
|
|