Solved

deleting duplicates in a sql table

Posted on 2016-09-26
4
37 Views
Last Modified: 2016-09-28
I have 7 columns.
i need rows to be deleted if there is a duplicate if all cells match between two rows on all 7 values.
is there an easy to do this?
0
Comment
Question by:jamesmetcalf74
  • 2
4 Comments
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
you could do the following:

1) Select all distinct rows into a new table
2) truncate your original table
3) copy rows from the table you created into the original table

SELECT DISTINCT col1,col2,col3
INTO   #staging
FROM   Table1

TRUNCATE TABLE Table1

INSERT INTO Table1 (col1,col2,col3)
SELECT col1,col2,col3
FROM #staging

Open in new window

0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
you could also create a new regular table with the same structure, insert distinct data in that table, then delete your original table and rename new table to the same name as original table had
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 total points
Comment Utility
WITH cteDistinct AS
(
	SELECT Col1, Col2, Col3, Col4, Col5, Col6, Col7,
		ROW_NUMBER() OVER(PARTITION BY Col1, Col2, Col3, Col4, Col5, Col6, Col7) AS RowNumber
	FROM myTable
)
DELETE cteDistinct
WHERE RowNumber > 1

Open in new window

1
 
LVL 16

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 250 total points
Comment Utility
Modified Brian Version  (Removed syntax errors)

--

WITH cteDistinct AS
(
	SELECT Col1, Col2, Col3, Col4, Col5, Col6, Col7,
		ROW_NUMBER() OVER(PARTITION BY Col1, Col2, Col3, Col4, Col5, Col6, Col7 ORDER BY (SELECT 1)) AS RowNumber
	FROM myTable
)
DELETE FROM cteDistinct
WHERE RowNumber > 1

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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.
Viewers will learn how the fundamental information of how to create a table.

728 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

12 Experts available now in Live!

Get 1:1 Help Now