Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL Server中获取包含第一行号的重复行?_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

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

如何在SQL Server中获取包含第一行号的重复行?,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我在下面编写了使用SQL Server中的Row_Number()检索重复客户的查询 Cust_PKID ---------------+ CustomerID ----------------- + MobileNo 1 | A00001 | 9000477444 2 | A00002 | 9000477

我在下面编写了使用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 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\u 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
记录

单次选择不可能,您必须使用函数/子查询,并在其中始终选择语句上面的查询需要更多的时间来执行。客户表中有近1000万条记录。我已经为该表创建了适当的索引。之前我使用了GROUPBY和Having Count(*)查询。它还需要更多的时间来执行。在哪个列上有索引。它被使用了吗?我在MobileNo上有索引,CustomerID在Customers表中。@RGS-我不擅长索引,但给我一些更多的信息,比如它是非聚集的还是聚集的?或者这两列都有单独的索引?它们都是单独的非聚集索引。我将调查执行计划。非常感谢。