Solved

Find duplicate fields in table

Posted on 2013-06-22
7
333 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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:
Ephraim Wangoya 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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 ?
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

685 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