I'm an SQL novice and I have a table with the following columns
+-------+--------+----+----+
| email | date | IP | ID |
+-------+--------+----+----+
And I want to do something like this:
SELECT T.email,
Max(T.date),
T.ip AS User_IP,
T.id AS ID
FROM LoginTable as T
WHERE (IP IS NOT NULL)
GROUP BY T.email
Of course this won't work because IP and ID are not in a (max) aggregate function. But I need that the IP matches the ID and the date, so I cannot use (max) in those columns because I would get results from different rows, and that's not an option.
To recap, I need:
- A single row per each unique email (hence the group by)
- The row selected is the one with the most recent date -> max(date)
- I need IP and ID to be from the same row as the row selected by max(date).