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.
| Author |
Topic |
|
Quailman808
Starting Member
2 Posts |
Posted - 2010-02-12 : 17:30:42
|
| Hello all! This is my first posting, but I'm hoping to gain some knowledge in the process. I'd like to execute an SQL Query that reaches out to multiple tables, joins them on their commonalities, and return only one (1) record for each user. Our DB is set up to record when a user logs in to a system, but the problem is that it holds on to a few of their last log ins for security reasons. I'd like to return just the most recent log-in date. The code I have included below is what I am currently working with, which returns each log-in session: SELECT id, lname, fname, people_stats.login_dateFROM mrt_user join mrt_employee on mrt_user.id = mrt_employee.uid join mrt_role on mrt_user.id = mrt_role.uid join mrt_people_stats on mrt_user.id = mrt_people_stats.uidORDER BY lname asc;Any help would be greatly appreciated! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-13 : 10:00:52
|
may be thisSELECT id, lname, fname, people_stats.login_dateFROM mrt_userjoin mrt_employee on mrt_user.id = mrt_employee.uidjoin mrt_role on mrt_user.id = mrt_role.uidjoin mrt_people_stats on mrt_user.id = mrt_people_stats.uidjoin (select uid,max(login_date) as recent from mrt_people_stats group by uid) tmpon tmp.uid= mrt_people_stats.uidand tmp.recent = mrt_people_stats.login_dateORDER BY lname asc; ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Quailman808
Starting Member
2 Posts |
Posted - 2010-02-15 : 11:04:44
|
| Visakh16, Thank you very much for the guidance! With very few changes I have been able to successfully return the requested information :) Hopefully I will be able to use this as a base to add additional pieces of information if they become necessary. Thank you again. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-15 : 11:11:00
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|