I'm trying to pull data out from a table. To simplify I have a table (time_entries ) has 3 colums user_name, entry_type and entry_datetime
Here is a sample output
user_name| entry_type | entry_datetime
User1 | Time In | 28-JUL-13 16:40:40
User1 | Time Out | 28-JUL-13 16:40:41
User2 | Time In | 28-JUL-13 16:41:13
User2 | Time Out | 28-JUL-13 16:41:15
User3 | Time In | 28-JUL-13 16:42:32
What I'm trying to do here is to pull the result when the last time each User logged in
MY QUERY
SELECT te.user_name, te.entry_type, MAX(te.entry_datetime) AS date
FROM time_entries AS te
GROUP BY te.user_name
this runs fine only with wrong results, here is the output below
OUTPUT
user_name| entry_type | entry_datetime
User1 | Time In | 28-JUL-13 16:40:41
User2 | Time In | 28-JUL-13 16:41:15
User3 | Time In | 28-JUL-13 16:42:32
user_name and entry_datetime is correct but the entry_type are all Time In. User1 and User2 entry_type must be Time Out.
Anyone knows a solution for this problem?