eliminating duplicates

  • what about:

    select distinct to a temp table

    truncate source table

    get the temp table data back in

    or

    select distinct from a to b

    drop a

    rename b to a

    ?

    there is no info about the size of the table, concurrency requirements or if it's a 24x7 environment (or not) ... (so again standard dba reply "it depends" ...

    *edit: sorry, now i see it's not a dedupe, somehow i missed such a simple point ...

  • You guys are missing the point of this particular exercise... despite the name of the post, this is NOT about the elimination of duplicates. Here's what the op wanted...

    1. Remove 1 row from any group of dupes.

    2. Remove all rows that are not dupes.

    3. Cannot use any form of intermediate table. No table variable, no temp table, not even the working table formed by a cursor.

    4. Cannot add any columns to the original table.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • It is not impossible. Add an identity column. Each record will be numbered. Then use the identity column to delete the duplicate records.

  • Jeff Moden (7/1/2008)


    You guys are missing the point of this particular exercise... despite the name of the post, this is NOT about the elimination of duplicates. Here's what the op wanted...

    1. Remove 1 row from any group of dupes.

    2. Remove all rows that are not dupes.

    3. Cannot use any form of intermediate table. No table variable, no temp table, not even the working table formed by a cursor.

    4. Cannot add any columns to the original table.

    Zackly. Get rid of everything which would stay if it was a dedupe.

    It's a NOT dedupe! πŸ˜€

    β€œ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

  • lmarcum (7/1/2008)


    It is not impossible. Add an identity column. Each record will be numbered. Then use the identity column to delete the duplicate records.

    Read the OP's second post, and Jeff's last post.

    No extra columns.

    You're loosely describing a dedupe. It's not a dedupe.

    β€œ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

  • the point of the exercise -

    ...so all that is left is a table full of records which were duplicated at least once!

    (and some of these maybe duplicated in the remaining rows).

    Misleading title though....;)

    Jeff, Chris; It falls on deaf ears, yet again πŸ˜€

    Hiding under a desk from SSIS Implemenation Work :crazy:

  • Shaun McGuile (7/1/2008)


    the point of the exercise -

    ...so all that is left is a table full of records which were duplicated at least once!

    Misleading title though....;)

    Jeff, Chris; It falls on deaf ears, yet again πŸ˜€

    Hahaha this must look horribly familiar to you Shaun!

    Bored now, I'm outta here. Learned some good stuff from Barry and Janine though. Top work!

    β€œ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

  • These eyes may be failing, but whats amazing is my poor ability to read english here. Its easier to explain things with input, output , result... See below for something thats a heck of a lot closer..

    (I reckon a misleading or unclear title/description really doesnt help; especially when trying to do something else..)

    USE [tempdb]

    GO

    --Create Table

    --drop table [my_tab]

    CREATE TABLE [dbo].[my_tab](

    [acctproc_id] [int] NOT NULL,

    [name] [varchar](255) NOT NULL,

    [acct_id] [int] null)

    -- Add clustered unique index, with IGNORE_DUP_KEY "ON"

    --create UNIQUE CLUSTERED index [my_tab_unique_row_removal] on [dbo].[my_tab]

    --( [acctproc_id],[name],[acct_id] ASC

    --)

    --WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

    --ON [PRIMARY]

    --Display table output (will be blank atm)

    select * from my_tab

    -- Insert some data, 3 rows, 1 dup row = 2 end rows in table

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 0 - Non-Duplicate', NULL )

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 1 - Duplicate', NULL )

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 1 - Duplicate', NULL )

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 1 - Duplicate', NULL )

    --Display table output : Will have 4 rows in. dup rows present

    select * from my_tab

    -- DELETE ALL Rows which are NOT Duplicates !

    delete from my_tab

    from my_tab a

    -- Get first duplicate record for delete

    join (select top 1 acctproc_id, [name], acct_id

    from dbo.my_tab

    group by acctproc_id, name, acct_id

    having count(*) =1) b on

    -- Cope with a NULL Join condition for row where null possible on acct_id

    (a.acct_id = b.acct_id or a.acct_id is null and b.acct_id is null)

    and a.name = b.name

    and a.acctproc_id = b.acctproc_id

    -- RUN THIS LOOP ! No TEMP TABLES, Table vars etc.. !

    -- GRAB a quick count check to see if any DUP rows exist

    while (select count(*)

    from dbo.my_tab

    group by acctproc_id, name, acct_id

    having count(*) >1) > 0

    begin -- Start our loop deleting the dup rows

    --set rowcount 1 -- ensure we only delete 1 row, cannot delete the two dup records

    -- We'll use the TOP Clause here, for a SQL 2005 mechanism, SQL 2000, use set rowcount 1

    delete top (1) from my_tab

    from my_tab a

    -- Get first duplicate record for delete

    join (select top 1 acctproc_id, [name], acct_id

    from dbo.my_tab

    group by acctproc_id, name, acct_id

    having count(*) > 1) b on

    -- Cope with a NULL Join condition for row where null possible on acct_id

    (a.acct_id = b.acct_id or a.acct_id is null and b.acct_id is null)

    and a.name = b.name

    and a.acctproc_id = b.acctproc_id

    end

    select * from my_tab

    -- LEAVES ONLY 1 COPY of ANY DUplicate ROW as wanted !!!

    -- INPUT:

    acctproc_idnameacct_id

    0Record 0 - Non-DuplicateNULL

    0Record 1 - DuplicateNULL

    0Record 1 - DuplicateNULL

    0Record 1 - DuplicateNULL

    OUPUT:

    acctproc_idnameacct_id

    0Record 1 - DuplicateNULL

  • Hate to tell you the bad news...

    ...sorry

    ...it was to remove all unique rows and the 1st of every duplicated row.

    your output needs 2 rows πŸ˜‰

    Sorry. πŸ™‚

    Hiding under a desk from SSIS Implemenation Work :crazy:

  • Heh... this all reminds me of another of my favorite 4 letter acronyms... [font="Arial Black"]RTFS[/font]!!!!! πŸ˜€

    Yeah... I'm out'a here, too! Nice job to the folks that actually read and solved the problem description actually posted by the OP! πŸ˜‰

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • d_sysuk (7/1/2008)


    LEAVES ONLY 1 COPY of ANY DUplicate ROW as wanted !!!

    Except that that is NOT what was wanted. What was wanted was:

    Eliminate One row from each distinct Group.

    With the Following restrictions:

    No intermediate tables!

    and

    No additional Identity columns

    SQL Server 2000 (not 2005)

    And, yes, we all know that it can be easily be done with Cursors or While loops. In fact, it seems like the point of this challenge was to try to demonstrate something that could only be done with Cursors or other kinds of loops. Therefore there is one additional consideration that you might as well add:

    No Cursors, and No Loops!

    Now try it ...

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • I felt the force in that one πŸ˜€

    sorry couldn't resist it darth rbarry.

    troll!troll!troll!;)

    Hiding under a desk from SSIS Implemenation Work :crazy:

  • Oh all right then. No funny stuff either.

    DECLARE @ItemID INT

    DECLARE @FirstCol varChar(5), @SecondCol INT, @ThirdCol Char(1)

    SET @ItemID = 1

    UPDATE #Testing SET ThirdCol = CASE @ItemID WHEN 1 THEN 'D' ELSE ThirdCol END,

    @ItemID = CASE WHEN @FirstCol = FirstCol AND @SecondCol = SecondCol AND @ThirdCol = ThirdCol THEN @ItemID+1 ELSE 1 END,

    @FirstCol = FirstCol, @SecondCol = SecondCol, @ThirdCol = ThirdCol

    DELETE FROM #Testing WHERE ThirdCol = 'D'

    β€œ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

  • Well going to the first post, its clear the the post is most unclear in its requirements. Shouting at posters wont help under any circumstances or imposing random conditions , e.g. its now SQL 2000, not SQL 2005 etc..

    There is something that makes posts clear, test data , source input / output - makes life easier to understand than a bunch of words.

    A test-framework helps any post, users know what the goals are..

    Anyways.. here is A LOOP ! - But it does whats required..

    INPUT:

    acctproc_idnameacct_id

    0Record 0 - Non-DuplicateNULL

    0Record 1 - DuplicateNULL

    0Record 1 - DuplicateNULL

    0Record 1 - DuplicateNULL

    0Record 3 - DuplicateNULL

    0Record 3 - DuplicateNULL

    OUTPUT:

    acctproc_idnameacct_id

    0Record 1 - DuplicateNULL

    0Record 1 - DuplicateNULL

    0Record 3 - DuplicateNULL

    0Record 3 - DuplicateNULL

    TASK: Remove first Duplicate row, Remove non-duplicate rows.

    CODE:

    USE [tempdb]

    GO

    --Create Table

    drop table [my_tab]

    go

    CREATE TABLE [dbo].[my_tab](

    [acctproc_id] [int] NOT NULL,

    [name] [varchar](255) NOT NULL,

    [acct_id] [int] null)

    -- Add clustered unique index, with IGNORE_DUP_KEY "ON"

    --create UNIQUE CLUSTERED index [my_tab_unique_row_removal] on [dbo].[my_tab]

    --( [acctproc_id],[name],[acct_id] ASC

    --)

    --WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

    --ON [PRIMARY]

    --Display table output (will be blank atm)

    select * from my_tab

    -- Insert some data, 3 rows, 1 dup row = 2 end rows in table

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 0 - Non-Duplicate', NULL )

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 1 - Duplicate', NULL )

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 1 - Duplicate', NULL )

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 1 - Duplicate', NULL )

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 3 - Duplicate', NULL )

    insert into dbo.my_tab (

    acctproc_id,

    [name],

    acct_id

    ) values ( 0, 'Record 3 - Duplicate', NULL )

    --Display table output : Will have 4 rows in. dup rows present

    select * from my_tab

    -- DELETE ALL Rows which are NOT Duplicates !

    delete from my_tab

    from my_tab a

    -- Get first duplicate record for delete

    join (select top 1 acctproc_id, [name], acct_id

    from dbo.my_tab

    group by acctproc_id, name, acct_id

    having count(*) =1 ) b on

    -- Cope with a NULL Join condition for row where null possible on acct_id

    (a.acct_id = b.acct_id or a.acct_id is null and b.acct_id is null)

    and a.name = b.name

    and a.acctproc_id = b.acctproc_id

    declare @myvarloop int

    set @myvarloop =0

    -- RUN THIS LOOP ! No TEMP TABLES, Table vars etc.. !

    -- GRAB a quick count check to see if any DUP rows exist

    while (select top 1 a.count_me

    from (

    select count(*) count_me, row_number() over (order by acctproc_id, name, acct_id) row

    from dbo.my_tab

    group by acctproc_id, name, acct_id

    having count(*) >1 a

    where

    a.row > @myvarloop) > 0

    begin

    -- GRAB the First Duplicate ROW Number !

    select @myvarloop = b.row

    from

    (select top 1 a.count_me, a.row

    from (

    select count(*) count_me, row_number() over (order by acctproc_id, name, acct_id) row

    from dbo.my_tab

    group by acctproc_id, name, acct_id

    having count(*) >1) a

    where

    a.row > @myvarloop) b

    select @myvarloop

    -- Start our loop deleting the dup rows

    -- set rowcount 1 -- ensure we only delete 1 row, cannot delete the two dup records

    -- We'll use the TOP Clause here, for a SQL 2005 mechanism, SQL 2000, use set rowcount 1

    delete top (1) from my_tab

    from my_tab a

    -- Get first duplicate record for delete

    join (select top 1 acctproc_id, [name], acct_id,

    row_number() over (order by acctproc_id, name, acct_id) row

    from dbo.my_tab

    group by acctproc_id, name, acct_id

    having count(*) > 1) b on

    -- Cope with a NULL Join condition for row where null possible on acct_id

    (a.acct_id = b.acct_id or a.acct_id is null and b.acct_id is null)

    and a.name = b.name

    and a.acctproc_id = b.acctproc_id

    and row = @myvarloop

    end

    select * from my_tab

  • d_sysuk: dude you just don't get it do you?

    Sorry. I'm not the politest of posters here.

    The thing is we all got bored with this question yesterday, and well although you get credit for trying,

    its becoming fanatical for you, sorry again if I'm being blunt.

    You have povided a working solution, kudos. Nobody doubts you ability.

    Please stop posting this thread.

    Hiding under a desk from SSIS Implemenation Work :crazy:

Viewing 15 posts - 61 through 75 (of 137 total)

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