• How to remove duplicate rows?





    The following query is useful to delete duplicate rows. The table in this example has ID as an identity column and the columns which have duplicate data are Column1Column2 and Column3.
    DELETE FROM TableName
    WHERE  ID NOT IN (select * from(SELECT MAX(ID)
                      FROM   TableName
                      GROUP  BY Column1,
                                Column2,
                                Column3
                      /*Even if ID is not null-able SQL Server treats MAX(ID) as potentially
                        nullable. Because of semantics of NOT IN (NULL) including the clause
                        below can simplify the plan*/
                      HAVING MAX(ID) IS NOT NULL)) as t 
    The following script shows usage of GROUP BYHAVINGORDER BY in one query, and returns the results with duplicate column and its count.
    SELECT YourColumnName,
           COUNT(*) TotalCount
    FROM   YourTableName
    GROUP  BY YourColumnName
    HAVING COUNT(*) > 1
    ORDER  BY COUNT(*) DESC 
  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari