Solved

Delete duplicate rows from table SQL 2008

Posted on 2010-09-01
7
751 Views
Last Modified: 2012-06-22
SQL 2008

I have a query that deletes duplicate row form SQL table. The query looks like this:

with CTE as (
      select *, row_number() over (partition by Time_stamp order by Time_stamp) rn
      from yourtable
)
delete from CTe where rn > 1;

Query works fine. I just need to expand it's function however. I need to be able to scroll thru all tables in the database and find duplicate rows (leaving just one behind). So I am thinking  to use a cursor.

Each table has a id colum called "Date_Stamp". Duplicate Rows are those that have matching id (keys), regardless of the reset of data in other columns.

How do I achieve that?
0
Comment
Question by:techzone12
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 33579473
But this will do the job though:

DELETE FROM mytable WHERE yourid IN
    (SELECT yourid
    FROM mytable
    GROUP BY yourid
    HAVING COUNT(*) > 1)
0
 

Author Comment

by:techzone12
ID: 33579577
Yes. It looks actually simpler.

Now I need to scroll thru all tables in the database and delete duplicate rows. How do I do this?

Thanks
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 33579696
>>DELETE FROM mytable WHERE yourid IN
    (SELECT yourid
    FROM mytable
    GROUP BY yourid
    HAVING COUNT(*) > 1) <<
Wrong, that will delete all rows where there are duplicates, including the one you want to leave behind!!
You can try the below if you want to avoid a cursor
 

exec sp_MSforeachtable '

if exists (select 1 from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME= ''?'' and COLUMN_NAME = ''Date_Stamp'')

	with CTE as (

	      select *, row_number() over (partition by Time_stamp order by Time_stamp) rn

	      from [?]

	)

	delete from CTe where rn > 1'

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33584205
Just a correction to the code by ralmada

exec sp_MSforeachtable '
if exists (select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME= ''?'' and COLUMN_NAME = ''Date_Stamp'')
begin
;with CTE as (
            select *, row_number() over (partition by Date_Stamp order by Date_Stamp) rn
            from [?]
      )
      delete from CTe where rn > 1
end'

Partition should be by date_stamp or whatever name your real column is, and with CTE requires a leading ;
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33584209
Further to the above, the ; is optional, but it must be a new statement block, hence the begin - end.
0
 

Author Comment

by:techzone12
ID: 33585287
Thanks for the feedback.  I will try the modified query according to your suggetion.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33587343
Yeah, the column name should be date_stamp. I just copied and pasted your query without checking the column name. So where it reads "Time_stamp" it should read "Date_Stamp"
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

15 Experts available now in Live!

Get 1:1 Help Now