gpt4 book ai didi

sql - 如何在 SQL Server 中获取包含第一行号的重复行?

转载 作者:行者123 更新时间:2023-12-03 03:18:16 29 4
gpt4 key购买 nike

我编写了以下查询,以在 SQL Server 中使用 Row_Number() 检索重复的客户。

 Cust_PKID ---------------+ CustomerID ----------------- + MobileNo
1 | A00001 | 9000477444
2 | A00002 | 9000477444
3 | A00003 | 9000477444

查询:-

Select TMP.CustID
From
(
Select CustomerID CustID,
Row_Number() Over(Partition By MobileNo Order By (Select Null)) As RowNo
From dbo.Customers
) TMP
Where TMP.RowNo > 1

输出:-

Cust_PKID ---------------+ CustomerID ----------------- + MobileNo    
2 | A00002 | 9000477444
3 | A00003 | 9000477444

如何在单个 select 语句中检索包括第一个 RowNo 记录的记录?

最佳答案

您正在寻找 COUNT() OVER() 窗口函数,而不是 ROW_NUMBER

Select TMP.CustID
From
(
Select CustomerID CustID,
COUNT(1) Over(Partition By MobileNo) As RowNo
From dbo.Customers
) TMP
Where TMP.RowNo > 1

这将带来所有重复的 MobileNo 记录

关于sql - 如何在 SQL Server 中获取包含第一行号的重复行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39463025/

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