Solved

How to find duplicates in SQL Server

Posted on 2016-11-18
3
42 Views
Last Modified: 2016-11-18
Hi guys,

I want to find duplicate records from SQL Server, however i want to include the ID field as well which is unique. For example
1 a b c d
2 a b c d
3 a b c d
4 g h i j

Open in new window

how write a query to find first three records along with their ids.

regards
0
Comment
Question by:shah36
3 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41892688
Try..

SELECT id, Col1,col2,col3,col4 FROM 
(
SELECT * , ROW_NUMBER() OVER (PARTITION BY COL1,COL2,COL3,col4 ORDER BY (SELECT 1)) rnk
FROM yourTable
)k
WHERE rnk > 1

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 41892691
Try using count() over()

select * from (
Select *
, count(*) over(partition by col1,col2,col3,col4) as counted
From that_table) d
Where counted > 1
0
 

Author Closing Comment

by:shah36
ID: 41892703
Thanks a lot for your help. yes it worked like a magic
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question