【发布时间】:2021-07-03 19:06:18
【问题描述】:
这是一个表的“玩具”示例,它有很多列和数千行。
我想要过滤掉任何包含相同 AcctNo、CustomerName 和 CustomerContact 的行,但保留 ONE 重复项的 ID(这样我以后可以访问记录) .
-
例子:
ID AcctNo CustomerName CustomerContact 1 1111 Acme Foods John Smith 2 1111 Acme Foods John Smith 3 1111 Acme Foods Judy Lawson 4 2222 YoyoDyne Inc Thomas Pynchon 5 2222 YoyoDyne Inc Thomas Pynchon <= I want to save IDs 2, 3, and 5
问:我需要什么 SQL 来完成这个?
【问题讨论】:
-
你试过什么?你在哪里卡住了?请向我们展示您的尝试。
-
ID 3 是怎么重复的?
-
你可以考虑使用
row_number()函数。 -
请分享你已经尝试过的sql。
-
您需要每个组的最大 ID...
标签: sql sql-server group-by distinct