How to get duplicate rows containing the first row number in SQL Server?

I wrote a query below to get duplicate clients using Row_Number () in SQL Server.

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

Query: -

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

Output: -

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

How can I get entries, including the first RowNo entry, in one ad?

+4
source share
1 answer

You are looking for COUNT() OVER()a window function notROW_NUMBER

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

This will result in duplicate entries. MobileNo

+3
source

Source: https://habr.com/ru/post/1654420/


All Articles