Results 1 to 3 of 3

Thread: Deleting duplicate rows

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574

    Deleting duplicate rows

    A very banal, a hackneyed issue, what's the query to delete duplicate rows from a table. For instance if my table has three rows with exactly the same data, then how do I delete two rows and just let one of them remain?

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    Do these rows are totally indentical or are is there (1 or more) field that is different (a PK = primary key). If there is no PK then it is not possible (I think) otherwise the query is possible:
    VB Code:
    1. select T1.ID , T2.ID from Table T1 where
    2. inner join Table1 T2
    3. T1.Field1 = T2.Field1
    4. T1.Field2 = T2.Field2
    5. T1.Field3 = T2.Field3
    6. T1.ID > T2.ID
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    don't know what DBMS you are using but for this i think i would:

    Create a second table with a schema that matches the first table (the one containing duplicates)

    Stick a unique constraint on one of the fields in the SECOND table.

    Create a stored proc using the appropriate language - TSQL or PL/SQL or whatever. This proc should populate a cursor with the contents of table 1 and then loop through attempting to insert into table 2.

    Catch the inevitable unique constraint exceptions in an error handling block, do nothing and then continue looping by using a resume statement, labels etc.

    Cheers...

    Just a thought - instead of the above why not just create the second table and populate it by using a SELECT DISTINCT * FROM TABLE_1 ?
    Last edited by powdir; Sep 17th, 2003 at 03:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width