我在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 | .......
+------+-----------+--------
我需要获得最大值为[id]的唯一[客户端ID],如此
+------+-----------+--------
| id | client id | bla bla blaaaa
+------+-----------+--------
| 3 | 3 | .......
| 4 | 8 | .......
| 6 | 15 | .......
| 7 | 1 | .......
| 8 | 16 | .......
| 9 | 2 | .......
| 10 | 9 | .......
| 12 | 12 | .......
+------+-----------+--------
我试过这段代码,但效果不好..有人能帮助我吗?
SELECT *
FROM table AS one
INNER JOIN table AS two
ON one.[client id] = two.[client id]
WHERE one.[id] > two.[id]
答案 0 :(得分:7)
SELECT max(id), client_id, blah_blah
FROM my_table
GROUP BY client_id, blah_blah