views:

37

answers:

2

Here's part of my table:

id   team_id  log_id
1         12       1
2         12       1
3         12       1
4         12       1
5          1       2
6          1       2
7          1       3
8          1       3

What query would produce this output (so only the records with the highest log_id values are returned that correspond to team_id)?

id   team_id  log_id
1         12       1
2         12       1
3         12       1
4         12       1
7          1       3
8          1       3
+2  A: 
SELECT *
FROM mytable t
WHERE log_id = (SELECT MAX(log_id) FROM mytable WHERE team_id = t.team_id)
cletus
This is the winner for simplicity and it just plain works. Thank you so much, cletus! You guys are fast!
MonkeyWrench32
A: 
SELECT id, team_id, log_id
  FROM table1 t2
  JOIN (SELECT team_id, MAX(log_id) max_log_id
          FROM table1
      GROUP BY team_id) t2 ON t1.team_id = t2.team_id 
                          AND t1.log_id = t2.max_log_id
najmeddine