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

Convoluted DeDuplicate Related Records Expand / Collapse
Author
Message
Posted Thursday, February 14, 2013 2:10 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, December 11, 2014 6:43 PM
Points: 2,838, Visits: 8,570
I'm not sure the right terms here, but I have a puzzle, that is easy to see, but I'm stuck on how to actually code for it. We have duplicate member records that have been identified, but the problem is that they are sort of many-to-many, so that duplicate #7 is a duplicate of #6, and #6 is a duplicate of #4, so I don't see how to make the link between #7 and #4.

Here's some EDITED sample data:

CREATE TABLE dbo.Duplicates_GD
(Ident int NOT NULL IDENTITY (1, 1),
MemIdNew int NOT NULL,
MemIdOld int NOT NULL ) ON [PRIMARY]

CREATE TABLE dbo.Members_GD
(MemID int NOT NULL,
JoinDate datetime NULL,
PaidDate datetime NULL) ON [PRIMARY]

insert into Members_GD (MemID, JoinDate, PaidDate) values(1, '01-01-2013', '01-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(2, '02-01-2013', '02-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(3, '03-01-2013', '03-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(4, '04-01-2013', '04-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(5, '05-01-2013', '05-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(6, '06-01-2013', '06-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(7, '07-01-2013', '07-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(8, '08-01-2013', '08-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(9, '09-01-2013', '09-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(10, '10-01-2013', '10-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(11, '11-01-2013', '11-15-2013')
insert into Members_GD (MemID, JoinDate, PaidDate) values(12, '12-01-2013', '12-15-2013')

insert into Duplicates_GD (MemIdNew, MemIdOld) values (11,5)
insert into Duplicates_GD (MemIdNew, MemIdOld) values(7,3)
insert into Duplicates_GD (MemIdNew, MemIdOld) values(9,7)
insert into Duplicates_GD (MemIdNew, MemIdOld) values(11,9)
insert into Duplicates_GD (MemIdNew, MemIdOld) values(5,1)

insert into Duplicates_GD (MemIdNew, MemIdOld) values(6,4)
insert into Duplicates_GD (MemIdNew, MemIdOld) values(10,8)
insert into Duplicates_GD (MemIdNew, MemIdOld) values(10,6)
insert into Duplicates_GD (MemIdNew, MemIdOld) values(8,2)
insert into Duplicates_GD (MemIdNew, MemIdOld) values(12,10)

select * from Duplicates_GD order by MemIdNew, MemIdOld
select * from Members_GD order by MemID


The even MemID (2,4,6,8,10,12) are related as a chain of records in the same "family" ie group1
and odd MemID 1 through 11 are related as a chain of records in the same "family", ie group2.

Goal: Update the oldest record's PaidDate from the most recent in each "family"
then delete all the records in each "family" except for the oldest, as below:

Select * from Members_GD

MemID JoinDate PaidDate
1 2013-01-01 2013-11-15
2 2013-01-08 2013-12-15

It is a one-time cleanup that needs to be done, so I'm not looking for the most efficient or elegant method, just something I can understand and possibly repeat if a similar problem is discovered later. I've been trying to populate temp tables and get distinct results, but not quite getting there.

Thoughts ?



Post #1420296
Posted Thursday, February 14, 2013 3:30 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:15 AM
Points: 5,317, Visits: 12,354
Here is one way, using running totals & an additional column.

1) Create a FamilyId column on Members_GD

alter table dbo.Members_GD
add FamilyId int

2) Populate the FamilyId column using grotty double quirky update thingy:

declare @FamilyId int = 1
declare @Changed int = 0

update Members_GD
set @FamilyId = FamilyId = @FamilyId + (
case
when @Changed = 0
then 0
else 1
end
)
,@Changed = (
case
when PaidDate is null
then 0
else 1
end
)

3) Now that's in place, all that is required is a simple GROUP BY:

select MemId = min(MemId)
,JoinDate = min(JoinDate)
,PaidDate = max(PaidDate)
from Members_GD
group by FamilyId




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.

It is better to keep your mouth shut and appear stupid than to open it and remove all doubt. (Mark Twain)
Post #1420326
Posted Thursday, February 14, 2013 6:51 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, December 11, 2014 6:43 PM
Points: 2,838, Visits: 8,570
Garbage In, Garbage Out.

I must apologize for wasting your time & not supplying representative data originally, so your solution worked based on what I posted earlier, but now i realize that many "families" of data do not have NULL dates.

Also, the MemID records are not sequential for a "family" of records. They are intermingled, so I made 1 group odd numbers, and one even numbers

So I've updated my original post.



Post #1420358
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse