Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL, get max id of column

I've got this table in MS SQL Server Management Studio

+------+-----------+--------
| id   | client id | bla bla blaaaa
+------+-----------+--------
|    1 |   1       | .......
|    2 |   2       | .......
|    3 |   3       | .......
|    4 |   8       | .......
|    5 |   9       | .......
|    6 |   15      | .......
|    7 |   1       | .......
|    8 |   16      | .......
|    9 |   2       | .......
|   10 |   9       | .......
|   12 |   12      | .......
+------+-----------+--------

I need to get unique [client id] with max value of [id], like this

+------+-----------+--------
| id   | client id | bla bla blaaaa
+------+-----------+--------
|    3 |   3       | .......
|    4 |   8       | .......
|    6 |   15      | .......
|    7 |   1       | .......
|    8 |   16      | .......
|    9 |   2       | .......
|   10 |   9       | .......
|   12 |   12      | .......
+------+-----------+--------

I tried this code, but it doesn't work well .. can someone help me?

SELECT *
FROM   table AS one 
INNER JOIN table AS two
   ON one.[client id] = two.[client id]
   WHERE one.[id] > two.[id]
like image 613
Nikolas Charalambidis Avatar asked Jan 21 '26 15:01

Nikolas Charalambidis


1 Answers

SELECT max(id), client_id, blah_blah
FROM my_table
GROUP BY client_id, blah_blah
like image 88
nick Avatar answered Jan 23 '26 07:01

nick