General information
Category:
Databases,Tables, Views, Indexing and SQL syntax
Title:
MAX() group by in SQL?
I need to do a SQL SELECT that keeps the ID and I need the last action date. The code to do that is as follows:
SELECT id, MAX(act_dt) FROM tbl GROUP BY id
OK, so far no problem. However, what if there are 10 action dates per ID and I also need to keep two more columns? That is, I need the exact values for the other two columns that correspond to the same row that is on the MAX(act_dt).
How can I be sure that I am getting say row 8 as the MAX date and the other two columns associated with that MAX date are also from the same row 8 not row 1 or 4 or 10?
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only