User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 403,518 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,844 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 759 | Replies: 2
Reply
Join Date: Jul 2008
Posts: 1
Reputation: ssprastogi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ssprastogi ssprastogi is offline Offline
Newbie Poster

Delete records from a table

  #1  
Jul 10th, 2008
how to delete duplicate records from table.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Location: USA
Posts: 56
Reputation: aboyd is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aboyd's Avatar
aboyd aboyd is offline Offline
Junior Poster in Training

Re: Delete records from a table

  #2  
Jul 10th, 2008
  1. DELETE FROM table1
  2. USING table1, table1 AS vtable
  3. WHERE (table1.ID > vtable.ID)
  4. AND (table1.field_name=vtable.field_name)

You may need to have lots of ANDs at the end, if you want lots of fields to be the same. If you just want to delete records that have ONE field the same, then my example works already.

As an example, let's adapt the SQL above to remove records from a table called macintosh. The table has many columns, but we're going to call a record a duplicate if three (of the many) columns match. Here we go:

  1. DELETE FROM macintosh
  2. USING macintosh, macintosh AS m2
  3. WHERE (macintosh.id > m2.id)
  4. AND (macintosh.manufacturer=m2.manufacturer)
  5. AND (macintosh.model=m2.model)
  6. AND (macintosh.os=m2.os)

PLEASE only try this on a copy of your database table. Do not run this on production tables until you've certified that it works.
Last edited by peter_budo : Jul 10th, 2008 at 8:36 am. Reason: Keep It Organized - please use [code] tags
Publisher Database - tools & forums for writers
Outshine - geek blog & free phpBB mods
What Do Women Want? - dating advice for men, from women
Reply With Quote  
Join Date: Feb 2008
Posts: 70
Reputation: cmhampton is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: Delete records from a table

  #3  
Jul 10th, 2008
If you are using SQL 2005, here's another option. (originally posted in http://www.daniweb.com/forums/post639423-7.html)

Suppose you have a table with the following structure:

id - int PrimaryKey
name - varchar(50)
description - varchar(MAX)

and the following values:

1	Joe	  Short for Joseph
2	Dave	  Short for David
3	Joe	  Short for Joseph
4	Joe	  Short for Joseph
5	Chris	  Short for Christian
6	Rob	  Short for Robert

Notice that "Joe - Short for Joseph" has three duplicate records. It is true that we can use SELECT DISTINCT to filter these, and for a simple table like this, that's probably the best option. However, sometimes SELECT DISTINCT gets a little hairy when dealing with joins, at least in my experience. So, without having the time to create a complex data structure, or using one I already have that contains confidential data, let's use this simple example.

MSSQL 2005 added a handy new function called ROW_NUMBER(). Learn it, love it (lol). Seriously though, it will make your life easier. What this function does is allow you to get the row number of a record in a returned data table. On the surface, this doesn't sound like much. But, it becomes extremely useful when you realize that you can partition, or group, the records. Let use this on the table shown above:

  1. WITH names AS
  2. (
  3. SELECT
  4. id,
  5. name,
  6. description,
  7. ROW_NUMBER() OVER(PARTITION BY name, description ORDER BY id) AS rowNum
  8. FROM
  9. table_1
  10. )
  11.  
  12. SELECT
  13. id,
  14. name,
  15. description,
  16. rowNum
  17. FROM
  18. names

We get these results:

id     name  description               rowNum

5	Chris	 Short for Christian    1
2	Dave	Short for David	        1
1	Joe	  Short for Joseph	 1
3	Joe	  Short for Joseph	 2
4	Joe	  Short for Joseph	 3
6	Rob	 Short for Robert	1

Notice that now we have rowNumbers for each record, and they are partitioned by the name and description fields. So, if we want to get the duplicate records for this table, we add a WHERE clause to the query:

  1. SELECT
  2. id,
  3. name,
  4. description
  5. FROM
  6. names
  7. WHERE
  8. rowNum > 1

which gives us:

3	Joe	Short for Joseph	2
4	Joe	Short for Joseph	3

Cool isn't it?

What this allows us to do, is dump this into a temporary table to use as in a subquery...

  1. DECLARE @tblNames TABLE(id int);
  2.  
  3. WITH names AS
  4. (
  5. SELECT
  6. id,
  7. name,
  8. description,
  9. ROW_NUMBER() OVER(PARTITION BY name, description ORDER BY id) AS rowNum
  10. FROM
  11. table_1
  12. )
  13. INSERT
  14. @tblNames
  15. SELECT
  16. id
  17. FROM
  18. names
  19. WHERE
  20. rowNum > 1
  21.  
  22. DELETE
  23. FROM
  24. table_1
  25. WHERE
  26. id IN
  27. (
  28. SELECT
  29. id
  30. FROM
  31. @tblNames
  32. )

Now if we select the records from table_1, the duplicates have been eliminated. Here's the results:

1	Joe	  Short for Joseph
2	Dave	Short for David
5	Chris	 Short for Christian
6	Rob	 Short for Robert

There's a way to do this without the temp table, but I can't find my original query, so I had to do it from memory.

Sorry to turn this into a novel, but I thought an explanation would be better than a code dump.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 1:07 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC