Solved

Find duplicate fields in table

Posted on 2013-06-22
7
331 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now