?
Solved

deleting duplicates in a sql table

Posted on 2016-09-26
4
Medium Priority
?
59 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 41816877
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
ID: 41816884
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 1000 total points
ID: 41816911
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 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 total points
ID: 41817149
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

801 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