?
Solved

How to remove duplicates when no primary key

Posted on 2012-09-14
6
Medium Priority
?
452 Views
Last Modified: 2012-10-02
Hi!

How can i remove duplicates from a table.
When i dont have a primary key ?
0
Comment
Question by:team2005
6 Comments
 
LVL 22

Expert Comment

by:Tapan Pattanaik
ID: 38397879
Hi team2005,

Please check this link, here is answer to your question.

Removing Duplicates from a Table in SQL Server:(Tables with no primary key)

http://www.simple-talk.com/sql/t-sql-programming/removing-duplicates-from-a-table-in-sql-server/
0
 
LVL 15

Expert Comment

by:Anuj
ID: 38397889
You can do he following way, if the the table is small and is not a part of replication, mirroring and Logshipping and does not have any references(Foreignkey)

DECLARE @t TABLE
(ID INT, C INT, C2 INT, C3 INT)

INSERT INTO @t
SELECT 1,1,2,4

INSERT INTO @t
SELECT 1,1,2,4
INSERT INTO @t
SELECT 1,1,2,4
INSERT INTO @t
SELECT 3,1,2,4
INSERT INTO @t
SELECT 1,2,2,4
INSERT INTO @t
SELECT 1,2,2,4


SELECT * FROM @t

-- in partition by clause, include all columns
SELECT ID,C,C2,C3
INTO #temp
FROM(
SELECT ROW_NUMBER() OVER(PARTITION BY ID, C, C2, C3 ORDER BY ID) RID,*
FROM @t
) T
WHERE RID = 1

DELETE FROM @t

INSERT INTO @t
SELECT * FROM #temp

SELECT * FROM @t


DROP TABLE #temp

Open in new window

0
 
LVL 22

Expert Comment

by:Tapan Pattanaik
ID: 38397893
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:Anuj
ID: 38397894
hm, @tapanpattanaik that link looks pretty good, Thanks!
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 38400181
You can simply partition by the fields you consider to be duplicates then delete from the cte directly, say field1, field2, field3 define your duplicates

;with ctedelete as
(
  select *, row_number() over (partition by field1, field2, field3 order by field1) rn
  from yourtable
)

delete 
from ctedelete
where rn > 1

Open in new window

0
 
LVL 2

Author Closing Comment

by:team2005
ID: 38454511
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

807 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