Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««56789»»»

eliminating duplicates Expand / Collapse
Author
Message
Posted Tuesday, July 1, 2008 6:03 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 28, 2014 8:38 AM
Points: 103, Visits: 215
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 ...
Post #526520
Posted Tuesday, July 1, 2008 6:35 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #526538
Posted Tuesday, July 1, 2008 6:50 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, July 21, 2014 8:13 AM
Points: 81, Visits: 105
It is not impossible. Add an identity column. Each record will be numbered. Then use the identity column to delete the duplicate records.
Post #526552
Posted Tuesday, July 1, 2008 6:52 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:46 AM
Points: 7,224, Visits: 13,696
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! :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
Exploring Recursive CTEs by Example Dwain Camps
Post #526556
Posted Tuesday, July 1, 2008 6:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:46 AM
Points: 7,224, Visits: 13,696
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
Post #526558
Posted Tuesday, July 1, 2008 7:06 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, October 4, 2012 9:20 AM
Points: 583, Visits: 1,060
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 :D


Hiding under a desk from SSIS Implemenation Work
Post #526578
Posted Tuesday, July 1, 2008 7:10 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:46 AM
Points: 7,224, Visits: 13,696
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 :D


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
Post #526587
Posted Tuesday, July 1, 2008 7:12 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 9, 2013 6:28 AM
Points: 37, 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
Post #526591
Posted Tuesday, July 1, 2008 7:25 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, October 4, 2012 9:20 AM
Points: 583, Visits: 1,060
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
Post #526607
Posted Tuesday, July 1, 2008 7:31 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
Heh... this all reminds me of another of my favorite 4 letter acronyms... RTFS!!!!! :D

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #526613
« Prev Topic | Next Topic »

Add to briefcase «««56789»»»

Permissions Expand / Collapse