Solved

deleting duplicates in a sql table

Posted on 2016-09-26
4
54 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 250 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 250 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-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

726 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