DUPLICATE Records

  • i have one table having three columns.This table contains lot of repeated records.

    i want to delete this records .

    In this below example i want to delete all the records

    which columns id and no columns contains same values.

    id no sequence

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

    35 35432 1

    35 35432 2

    35 35432 3

    36 35432 1

    35 45623 1

    first three records the columns id and no contains same value.

    i want to delete this three records.

    but in last record for id =35 and no column =45623.it is not repeated so it should not be

    deleted.

  • jkramprakash (9/24/2015)


    i have one table having three columns.This table contains lot of repeated records.

    i want to delete this records .

    In this below example i want to delete all the records

    which columns id and no columns contains same values.

    id no sequence

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

    35 35432 1

    35 35432 2

    35 35432 3

    36 35432 1

    35 45623 1

    first three records the columns id and no contains same value.

    i want to delete this three records.

    but in last record for id =35 and no column =45623.it is not repeated so it should not be

    deleted.

    This is TSQL Level 101, and there are numerous ways of going about it. What have you tried so far?

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • i tried below query

    DELETE FROM [TableName]

    WHERE id IN(

    SELECT id

    FROM [TableName]

    GROUP BY id

    HAVING COUNT(id) > 1)

    but it is checking only ID column not NO column.I want to check both columns.

  • Maybe something like,

    DELETE FROM [TableName] WHERE id IN(

    SELECT id FROM [TableName] GROUP BY id HAVING COUNT(id) > 1

    UNION

    SELECT id FROM [TableName] WHERE [no] IN (SELECT [no] FROM [TableName] GROUP BY [no] HAVING COUNT([no]) > 1)

    )

  • jkramprakash (9/24/2015)


    . . . first three records the columns id and no contains same value.

    i want to delete this three records.

    but in last record for id =35 and no column =45623.it is not repeated so it should not be

    deleted.

    Try this:

    declare @t table

    (

    id int,

    num int,

    seq int

    )

    insert @t values

    (35, 35432, 1),

    (35, 35432, 2),

    (35, 35432, 3),

    (36, 35432, 1),

    (35, 45623, 1)

    ;

    with cte as

    (

    select id, num, count(*) dup_cnt

    from @t

    group by id, num

    )

    delete t

    --select *

    from @t t

    inner join cte c on c.id = t.id and c.num = t.num

    where c.dup_cnt > 1

    select * from @t

    If you don't like the cte, you can move it into the inner join.

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Or avoid the JOINs.

    WITH CTE AS

    (

    SELECT id, num, COUNT(*) OVER(PARTITION BY id, num) dup_cnt

    FROM @t

    )

    DELETE c

    --SELECT *

    FROM cte c

    WHERE c.dup_cnt > 1;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (9/24/2015)


    Or avoid the JOINs.

    ๐Ÿ˜Ž

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thank you

  • jkramprakash (9/25/2015)


    Thank you

    You're welcome.

    However, do you understand the solutions?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply