Delete records from a table

Reply

Join Date: Jul 2008
Posts: 1
Reputation: ssprastogi is an unknown quantity at this point 
Solved Threads: 0
ssprastogi ssprastogi is offline Offline
Newbie Poster

Delete records from a table

 
0
  #1
Jul 10th, 2008
how to delete duplicate records from table.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 56
Reputation: aboyd is an unknown quantity at this point 
Solved Threads: 0
aboyd's Avatar
aboyd aboyd is offline Offline
Junior Poster in Training

Re: Delete records from a table

 
0
  #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 9: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 Quick reply to this message  
Join Date: Feb 2008
Posts: 72
Reputation: cmhampton is an unknown quantity at this point 
Solved Threads: 10
cmhampton's Avatar
cmhampton cmhampton is offline Offline
Junior Poster in Training

Re: Delete records from a table

 
0
  #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:

  1. id - int PrimaryKey
  2. name - varchar(50)
  3. description - varchar(MAX)

and the following values:

  1. 1 Joe Short FOR Joseph
  2. 2 Dave Short FOR David
  3. 3 Joe Short FOR Joseph
  4. 4 Joe Short FOR Joseph
  5. 5 Chris Short FOR Christian
  6. 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:

  1. id name description rowNum
  2.  
  3. 5 Chris Short FOR Christian 1
  4. 2 Dave Short FOR David 1
  5. 1 Joe Short FOR Joseph 1
  6. 3 Joe Short FOR Joseph 2
  7. 4 Joe Short FOR Joseph 3
  8. 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:

  1. 3 Joe Short FOR Joseph 2
  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. 1 Joe Short FOR Joseph
  2. 2 Dave Short FOR David
  3. 5 Chris Short FOR Christian
  4. 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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC