| SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it. |
|
|||
How to find distinct users based on last logonassume a table as follows:
User(Id) LastOn(Date) Duration(long) U1 May 4 10 minutes U1 May 5 5 minutes U2 May 1 12 minutes U2 May 3 27 minutes I'm looking for a query that'd give me U1 May 1 5 minutes U2 May 3 27 minutes |
|
|||
SELECT * FROM users t0 WHERE laston=(SELECT max(t1.laston) FROM users t1 WHERE t0.id=t1.id);Discussion: if the user has logged on multiple times the same last logon day, then there would be multiple rows in the result for that user and you need more criteria to differentiate between those rows. Hoe this helps, Dimitar |
|
|||
|
Hi, on similar lines, if I have a table with accounts, users and last_on_date, how can I find the user who accessed an account most recently.
Say, the table is as follows: a/c user date ---- ---- ----------- a1 u1 03/04/2007 a1 u2 03/05/2007 a2 u3 03/05/2007 a2 u1 03/07/2007 a2 u2 03/06/2007 a3 u3 04/05/2007 What query would fetch the following: a/c user date ---- ---- ----------- a1 u2 03/05/2007 a2 u1 03/07/2007 a3 u3 04/05/2007 Any help is appreciated. Orshoe |