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 «««678910»»»

eliminating duplicates Expand / Collapse
Author
Message
Posted Tuesday, July 1, 2008 7:38 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
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 ...


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #526620
Posted Tuesday, July 1, 2008 7:41 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
I felt the force in that one :D

sorry couldn't resist it darth rbarry.

troll!troll!troll!;)


Hiding under a desk from SSIS Implemenation Work
Post #526627
Posted Tuesday, July 1, 2008 8:05 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 6,721, Visits: 13,832
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
Exploring Recursive CTEs by Example Dwain Camps
Post #526653
Posted Tuesday, July 1, 2008 8:21 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
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_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
0 Record 3 - Duplicate NULL
0 Record 3 - Duplicate NULL

OUTPUT:
acctproc_id name acct_id
0 Record 1 - Duplicate NULL
0 Record 1 - Duplicate NULL
0 Record 3 - Duplicate NULL
0 Record 3 - Duplicate NULL

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

Post #526672
Posted Tuesday, July 1, 2008 8:31 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
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
Post #526685
Posted Tuesday, July 1, 2008 8:41 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, April 19, 2011 7:31 AM
Points: 2,666, Visits: 5,316
As far as a framework, Jeff Moden posts this link in his signature (and I believe others do as well).
It makes it very clear what should be posted to receive quicker, more accurate responses. As far as SQL2000 vs. SQL2005, sometimes it's critical as there are changes/additions to functionality depending on version, correct? Sometimes posts are posted to the wrong forum and if people are thinking it's 2005, that's what most - if not all, responses will be geared toward. I don't like wasting my time trying to do something one way and find out, due to version, it's not going to work. And yes, sometimes, shouting at posters is the only way to be HEARD. The important thing is that people are giving their time, and talent, freely here to try and help someone out. Sometimes the OP also has to help themselves....IMHO

http://www.sqlservercentral.com/articles/Best+Practices/61537/


-- You can't be late until you show up.
Post #526690
Posted Tuesday, July 1, 2008 9:02 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
To the lovely Shawn: d_sysuk: dude you just don't get it do you?
--> Stop posting, we're fed up:..had enough of this yesterday.
Advice: STOP READING POST !

I like many others do not live on USA timezones, picked this question up my time today.

Had a look at this only for a couple of hours, but this highlights an interesting issue - almost valid of a MS connect feedback scenario - "To allow row_number deletes to apply - not to delete ALL duplicate entrys" ..., where it would be achieve a single SQL 2005 statement version :


delete from dbo.my_tab
from (select row_number() over (order by z.acctproc_id, z.[name], z.acct_id) row,
acctproc_id, name, acct_id
from my_tab z) SOURCE
JOIN
( select a.acctproc_id, a.[name], a.acct_id,
-- a.row,
c.count_row,
max(a.row) row_to_delete
from ( select z.acctproc_id, z.[name], z.acct_id,
row_number() over (order by z.acctproc_id, z.[name], z.acct_id) row
from dbo.my_tab z) a
join
(select b.acctproc_id, b.name, b.acct_id,count(*) count_row
from dbo.my_tab b
group by b.acctproc_id, b.name, b.acct_id
) c
on c.acctproc_id = a.acctproc_id
and c.name = a.name
and (c.acct_id = a.acct_id or c.acct_id is null and a.acct_id is null)
group by a.acctproc_id, a.name, a.acct_id, c.count_row) FILTEREDSET ON
--SOURCE.acctproc_id = FILTEREDSET.acctproc_id and
--SOURCE.[name] = FILTEREDSET.[name]
--and (SOURCE.acct_id = FILTEREDSET.acct_id
-- or Source.acct_id is null and filteredset.acct_id is null)
--and
SOURCE.row= FILTEREDSET.row_to_delete

to get the results we expect.
Where it is using the actual row_numer to perform the inline delete.
Post #526716
Posted Tuesday, July 1, 2008 9:40 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
d_sysuk: I am not in the USA, I'm ahead of them which could mean 16 hours behind :D as the earth spins. that aside your SQL2005 solution looks quite minimal in size.

implying 2005 > 2000 and not just numerically ;)

but original question was SQL2000

:D Shaun not Shawn :D


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


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, September 18, 2014 7:50 PM
Points: 5,322, Visits: 25,235
per rbarryyoung:
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)


This will delete all non duplicated entries and the first row of a duplicated entry - leaving all other duplicated rows in the table.

;with numbered as(SELECT rowno=row_number() over
(partition by [your column] order by [your column name] ), [your column name] from [your table] )
delete from numbered where rowno =1



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #526771
Posted Tuesday, July 1, 2008 10:55 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 6:06 PM
Points: 35,218, Visits: 31,678
bitbucket (7/1/2008)
per rbarryyoung:
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)


This will delete all non duplicated entries and the first row of a duplicated entry - leaving all other duplicated rows in the table.

;with numbered as(SELECT rowno=row_number() over
(partition by [your column] order by [your column name] ), [your column name] from [your table] )
delete from numbered where rowno =1


Heh... RTFS! That won't work in SQL Server 2000 as requested!


--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 #526802
« Prev Topic | Next Topic »

Add to briefcase «««678910»»»

Permissions Expand / Collapse