Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to delete duplicate records within a SQL Server 2008 table using a SQL statement?

Posted on 2016-11-28
5
Medium Priority
?
68 Views
Last Modified: 2016-11-29
I am working with a SQLServer 2008 database.

I have a table named "tblBank" with the following 5 fields:

bankID (varchar(50),null)
bankDescr(varchar(50),null)
status(varchar(50),null)
entity(varchar(50),null)
bankAcctNum(varchar(50),null)

Is there a SQL statement to delete any duplicate records within this table?
0
Comment
Question by:zimmer9
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 41904624
WITH cteDuplicate AS
(
	SELECT bankID, bankDescr, status, entity, bankAcctNum,
		ROW_NUMBER() OVER(PARTITION BY bankID, bankDescr, status, entity, bankAcctNum) AS RowNumber
	FROM tblBank
)
DELETE cteDuplicate
WHERE RowNumber > 1

Open in new window

0
 

Author Comment

by:zimmer9
ID: 41904637
Msg 4112, Level 15, State 1, Line 4
The ranking function "ROW_NUMBER" must have an ORDER BY clause.
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 41904678
There was nothing obvious to ORDER BY but you can just throw any column in there if it doesn't matter which record you keep.
1
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41905810
In addition to Brian's correct answer check out the code and image-heavy tutorial SQL Server Delete Duplicate Rows.  Also if you have any feedback please add it to the bottom of the article.  Thanks in advance.
1
 

Author Comment

by:zimmer9
ID: 41905820
Thanks gentlemen for your outstanding answer and suggestion.  :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
A couple of weeks ago, my client requested me to implement a SSIS package that allows them to download their files from a FTP server and archives them. Microsoft SSIS is the powerful tool which allows us to proceed multiple files at same time even w…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

971 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