Solved

Find duplicate fields in table

Posted on 2013-06-22
7
332 Views
Last Modified: 2013-06-24
Hello there,

I am trying to find duplicate fields in my table. I am using mssql 2008
I tried something like this but it does not return any records.

SELECT province,county,districtNcity,citycode,count(*) 
FROM fdocode
group by province,county,districtNcity,citycode
having count(*) > 1

Open in new window


BUT when I try this it returns many records

SELECT districtNcity,count(districtNcity) 
FROM fdocode
group by districtNcity
having count(districtNcity) > 1

Open in new window


I want the query to return all the cols and find duplicates in all the cols.

cheers
Zolf
0
Comment
Question by:zolf
  • 4
  • 2
7 Comments
 

Author Comment

by:zolf
ID: 39268922
I tried something like this and it worked to some extent. But I need to compare 2 cols for duplicate now it is only comparing one col i.e. the districtNcity col. I need to compare the districtNcity and county col together.if they are same then return all the col record

SELECT DISTINCT A.ID,province,county, A.districtNcity, A.citycode, B.Count
FROM fdocode A
JOIN (
  SELECT COUNT(*) as Count, B.districtNcity
  FROM fdocode B
  GROUP BY B.districtNcity
) AS B ON A.districtNcity = B.districtNcity
WHERE B.Count > 1
ORDER by A.districtNcity;

Open in new window

0
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 250 total points
ID: 39268931
If i understand you correctly...

SELECT A.*
FROM fdocode AS A
INNER JOIN (
   SELECT districtNcity, county
   FROM fdocode
   GROUP BY districtNcity, county
   HAVING COUNT(*) > 1 ) AS B
   ON A.districtNcity = B.districtNcity
      AND A.county = B.county
0
 

Author Comment

by:zolf
ID: 39268944
thanks a lot. it worked as expected. now if I want to delete the duplicate records how can I do it
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39268947
Since you don't appear to have a unique value in your record (IDENTITY, GUID) you will have to define which record you want to keep.
0
 

Author Comment

by:zolf
ID: 39268984
I do have PK. my table structure is as follows

CREATE TABLE
    fdocode
    (
        id INT NOT NULL IDENTITY,
        province NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        county NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        districtNcity NVARCHAR(255) COLLATE Latin1_General_CI_AS,
        citycode FLOAT(53)
    );

Open in new window

0
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 39268995
Use this

with cte as
(
 select *, row_number() over (partition by districtNcity, county order by ID) [RN]
 from fdocode
)

delete from cte
where RN > 1

Open in new window

0
 

Author Closing Comment

by:zolf
ID: 39273819
cheers
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 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