SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


eliminating duplicates


eliminating duplicates

Author
Message
Eduard
Eduard
SSC-Addicted
SSC-Addicted (417 reputation)SSC-Addicted (417 reputation)SSC-Addicted (417 reputation)SSC-Addicted (417 reputation)SSC-Addicted (417 reputation)SSC-Addicted (417 reputation)SSC-Addicted (417 reputation)SSC-Addicted (417 reputation)

Group: General Forum Members
Points: 417 Visits: 217
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 ...
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)

Group: General Forum Members
Points: 205009 Visits: 41952
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
lmarcum
lmarcum
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 109
It is not impossible. Add an identity column. Each record will be numbered. Then use the identity column to delete the duplicate records.
ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39520 Visits: 19991
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! BigGrin

“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
Exploring Recursive CTEs by Example Dwain Camps
ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39520 Visits: 19991
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
Exploring Recursive CTEs by Example Dwain Camps
Shaun McGuile
Shaun McGuile
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1079 Visits: 1060
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....Wink

Jeff, Chris; It falls on deaf ears, yet again BigGrin

Hiding under a desk from SSIS Implemenation Work Crazy
ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39520 Visits: 19991
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....Wink

Jeff, Chris; It falls on deaf ears, yet again BigGrin


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
Exploring Recursive CTEs by Example Dwain Camps
d_sysuk
d_sysuk
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 Visits: 227
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_id name acct_id
0 Record 0 - Non-Duplicate NULL
0 Record 1 - Duplicate NULL
0 Record 1 - Duplicate NULL
0 Record 1 - Duplicate NULL

OUPUT:
acctproc_id name acct_id
0 Record 1 - Duplicate NULL
Shaun McGuile
Shaun McGuile
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1079 Visits: 1060
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 Wink

Sorry. Smile

Hiding under a desk from SSIS Implemenation Work Crazy
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)

Group: General Forum Members
Points: 205009 Visits: 41952
Heh... this all reminds me of another of my favorite 4 letter acronyms... RTFS!!!!! BigGrin

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! Wink

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search