Link to home
Start Free TrialLog in
Avatar of gosi75
gosi75Flag for Iceland

asked on

find duplicated rows from many columns

I have a query that takes few columns and I need to find if there are any duplicates rows, how would the where clause be:

Select col1,col2,col3
from myTable
where (all rows are the same)

result example:

        col1    col2   col3
1      k10     101   John
2      k10     101   John
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image


Select A.ID, A.col1,A.col2,A.col3
from myTable A
inner join MyTable B on (A.col1=B.col1 and A.col2=B.col2 and A.col3=B.col3 and A.ID <> B.ID)
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can do a basic select as below

SELECT ROW_NUMBER() OVER (PARTITION BY col1, col2,Col3, ORDER BY col1, col2,Col3) as RN, Col1, Col2 ,Col3
FROM TableName

Or you can also use Rank() Over instead of Row_Number() Function
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gosi75

ASKER

I had to make some modifications to make it work, but this query works finally :)
with CTE
as
(  
   Select col1,col2,col3 ,ROW_NUMBER() 
   OVER (PARTITION BY col1,col2,col3
   ORDER BY col1,col2,col3) RN
   from myTable
)

select RN,col1,col2,col3
from CTE
where RN > 1

Open in new window