how to delete duplicate in all rows ?

Con risposta how to delete duplicate in all rows ?

  • giovedì 12 aprile 2012 14:23
     
     

    how to delete duplicate in all rows ?

    example:              

    column : number             address   before               number          address       after                         

                     1                   street1                                1                street1                                           

                     2                   street2                                2                street2

                     2                   street2                                3                  street4

                        3                     street4                               

    help query                                                                                                           







    • Modificato srajmuneer giovedì 12 aprile 2012 14:24
    • Modificato srajmuneer giovedì 12 aprile 2012 14:24
    • Modificato srajmuneer giovedì 12 aprile 2012 14:24
    • Modificato srajmuneer giovedì 12 aprile 2012 14:24
    • Modificato srajmuneer giovedì 12 aprile 2012 14:35
    • Modificato srajmuneer giovedì 12 aprile 2012 14:36
    •  

Tutte le risposte

  • giovedì 12 aprile 2012 14:26
    Moderatore
     
     

    If you are using SS 2005 or greater, then use the function ROW_NUMBER to identify those rows.

    with R as (
    select row_number() over(partition by number order by (select NULL)) as rn
    from T
    )
    delete R
    where rn > 1;

    As always, make a db backup before applying changes in batches.


    AMB

    Some guidelines for posting questions...

  • giovedì 12 aprile 2012 14:26
     
     

    Pretty good example here:

    http://support.microsoft.com/kb/139444


    Chuck

  • giovedì 12 aprile 2012 14:27
     
     Risposta suggerita

    ;with cte as

    (

    select *,row_number()over(partition by number,address order by id) as r  from yrtable

    )

    delete from cte where r>1


    Thanks and regards, Rishabh , Microsoft Community Contributor

  • giovedì 12 aprile 2012 14:42
     
     Con risposta

    Hi Friend,

    Just try with this:

     ---------------------------------------------------------------------------------

    DECLARE @table TABLE (data VARCHAR(20))
    INSERT INTO @table VALUES ('Rahul')
    INSERT INTO @table VALUES ('Vairagi')
    INSERT INTO @table VALUES ('Vairagi')
    INSERT INTO @table VALUES ('Kumar')
    INSERT INTO @table VALUES ('Rahul')
    INSERT INTO @table VALUES ('Kumar')
    INSERT INTO @table VALUES ('Rahul')
    INSERT INTO @table VALUES ('Vairagi')
    INSERT INTO @table VALUES ('Rahul')

     --Select * from @table

    SET NOCOUNT ON
    SET ROWCOUNT 1

    WHILE 1 = 1
       BEGIN
          DELETE   FROM @table
          WHERE    data IN (SELECT  data
                                   FROM    @table
                                   GROUP BY data
                                   HAVING  COUNT(*) > 1)
          IF @@Rowcount = 0
             BREAK ;
       END
    SET ROWCOUNT 0

    --Select * from @table

     ---------------------------------------------------------------------------------------------

    This will remove all the duplicates from a table.


    Please Mark as Answer if my post solved your problem and Vote As Helpful if the post was useful. ====================Blogs: www.sqlserver2005forum.blogspot.com ==============

    • Contrassegnato come risposta srajmuneer sabato 14 aprile 2012 19:24
    •  
  • giovedì 12 aprile 2012 14:49
    Moderatore
     
     Con risposta

    Hi Friend,

    Just try with this:

     ---------------------------------------------------------------------------------

    DECLARE @table TABLE (data VARCHAR(20))
    INSERT INTO @table VALUES ('Rahul')
    INSERT INTO @table VALUES ('Vairagi')
    INSERT INTO @table VALUES ('Vairagi')
    INSERT INTO @table VALUES ('Kumar')
    INSERT INTO @table VALUES ('Rahul')
    INSERT INTO @table VALUES ('Kumar')
    INSERT INTO @table VALUES ('Rahul')
    INSERT INTO @table VALUES ('Vairagi')
    INSERT INTO @table VALUES ('Rahul')

     --Select * from @table

    SET NOCOUNT ON
    SET ROWCOUNT 1

    WHILE 1 = 1
       BEGIN
          DELETE   FROM @table
          WHERE    data IN (SELECT  data
                                   FROM    @table
                                   GROUP BY data
                                   HAVING  COUNT(*) > 1)
          IF @@Rowcount = 0
             BREAK ;
       END
    SET ROWCOUNT 0

    --Select * from @table

     ---------------------------------------------------------------------------------------------

    This will remove all the duplicates from a table.


    Please Mark as Answer if my post solved your problem and Vote As Helpful if the post was useful. ====================Blogs: www.sqlserver2005forum.blogspot.com ==============

    Here you are deleting one row at a time, so think about performance if you need to delete thousands of rows. On the other hand, the use of SET ROWCOUNT will not affect INSERT / UPDATE / DELETE in future versions of SQL Server.

    http://msdn.microsoft.com/en-us/library/ms188774.aspx


    AMB

    Some guidelines for posting questions...

  • giovedì 12 aprile 2012 15:33
     
     Risposta suggerita
    One comment - if you added a primary key to your table it would make this a lot easier.  All tables should have some sort of unique key

    Chuck

  • sabato 14 aprile 2012 19:29
     
     

    thank you Rahul Vairagi and Hunchback yours solutions