gpt4 book ai didi

sql-server - SQL,获取列的最大ID

转载 作者:行者123 更新时间:2023-12-04 23:57:34 26 4
gpt4 key购买 nike

我在 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] 的唯一 [client 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]

最佳答案

SELECT max(id), client_id, blah_blah
FROM my_table
GROUP BY client_id, blah_blah

关于sql-server - SQL,获取列的最大ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31409541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com