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 123»»»

Consolidating records - TSQL problem Expand / Collapse
Author
Message
Posted Monday, September 24, 2012 9:59 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 3:26 AM
Points: 708, Visits: 3,287
Good afternoon,

I've been working on a project to eliminate duplicates from a record set containing contact and address details. (ChrisM@Work/Home - thank you so much for all your help!!!!!).

I have two columns in my final result set, one containing the ID of the record that I'm keeping and another containing the ID of thr row I am dropping.

To concolidate my final result set I've come up with the following solution but I know it's not great because I'm having to run it several times to complete the concolidation:

Let me explain with some sample code:


create table testing (retained int, dropped int)

insert into testing (retained, dropped)
select 767884, 157441 union all
select 1046261, 157441 union all
select 1055257, 157441 union all
select 157441, 73635 union all
select 767884, 73635 union all
select 1046261, 73635 union all
select 1055257, 73635 union all
select 1046261, 767884 union all
select 1055257, 767884 union all
select 1055257, 1046261

select * from testing

-- consoidate records:

-- updates 6 records:

update b
set b.retained=a.retained
from testing as a
inner join testing as b
on b.retained = a.dropped

-- updates remaining 3

update b
set b.retained=a.retained
from testing as a
inner join testing as b
on b.retained = a.dropped

select * from testing

drop table testing

See below image, left table is what I started with, right table is the result I want:



My solution works but I'm sure there is a better way to do this. Any suggestions to do this in one pass?!

Thanks in advance.


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1363589
Posted Monday, September 24, 2012 10:26 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:47 PM
Points: 23,293, Visits: 32,028
I came up this:


create table testing (retained int, dropped int)

insert into testing (retained, dropped)
select 767884, 157441 union all
select 1046261, 157441 union all
select 1055257, 157441 union all
select 157441, 73635 union all
select 767884, 73635 union all
select 1046261, 73635 union all
select 1055257, 73635 union all
select 1046261, 767884 union all
select 1055257, 767884 union all
select 1055257, 1046261

select * from testing
ORDER BY dropped,retained;

WITH UpdateValues AS (
SELECT
MAX(retained) AS NewValue,
dropped
FROM
testing
GROUP BY
dropped
)
UPDATE t SET
retained = uv.NewValue
FROM
testing t
INNER JOIN UpdateValues uv
ON (t.dropped = uv.dropped);


select * from testing
ORDER BY dropped,retained;

DROP TABLE testing;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1363608
Posted Monday, September 24, 2012 10:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 2:06 PM
Points: 5,047, Visits: 11,797
Lynn Pettis (9/24/2012)
I came up this:
--snip


Interesting image that conjures up. I didn't take in the rest of your post.



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.
Post #1363611
Posted Monday, September 24, 2012 10:31 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:47 PM
Points: 23,293, Visits: 32,028
Phil Parkin (9/24/2012)
Lynn Pettis (9/24/2012)
I came up this:
--snip


Interesting image that conjures up. I didn't take in the rest of your post.


Not feeling well today?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1363613
Posted Monday, September 24, 2012 10:32 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 3:26 AM
Points: 708, Visits: 3,287
Nicely done Lynn,

I started doing the CTE but you beat me to it.....

Much appreciated!


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1363615
Posted Tuesday, September 25, 2012 2:24 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 3:26 AM
Points: 708, Visits: 3,287
Lynne's solution works if we assume that the retained ID is always the maximum.

But tthe solution doesn't work with the following record set:

drop table dbo.testing 
create table dbo.testing (retained int, dropped int)

insert into dbo.testing (retained, dropped)
select 767884, 157441 union all
select 1046261, 157441 union all
select 6699, 157441 union all
select 157441, 73635 union all
select 767884, 73635 union all
select 1046261, 73635 union all
select 6699, 73635 union all
select 1046261, 767884 union all
select 6699, 767884 union all
select 6699, 1046261

I will keep trying to see if I can come up with a solution but if anyone else can think of something then that'd be great!

Thanks in advance.


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1363850
Posted Tuesday, September 25, 2012 2:33 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:46 AM
Points: 7,224, Visits: 13,696
Hi Abu, thank you for the kind mention - I take it that phase of the project is now concluded.

Have a try with this. It works by first selecting retained rows which don't get a mention in discarded rows, then left joining to self. Hope that makes sense

SELECT a.* 
FROM testing a
LEFT JOIN testing b ON b.retained = a.dropped
WHERE NOT EXISTS (SELECT 1 FROM testing i WHERE a.retained = i.dropped)



“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 #1363855
Posted Tuesday, September 25, 2012 4:59 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 3:26 AM
Points: 708, Visits: 3,287
ChrisM@Work (9/25/2012)
Hi Abu, thank you for the kind mention - I take it that phase of the project is now concluded.


Hi Chris, not quite! It's a looong story lol .... will explain another time


Have a try with this. It works by first selecting retained rows which don't get a mention in discarded rows, then left joining to self. Hope that makes sense

SELECT a.* 
FROM testing a
LEFT JOIN testing b ON b.retained = a.dropped
WHERE NOT EXISTS (SELECT 1 FROM testing i WHERE a.retained = i.dropped)



Not sure I get you.

Here is another sample record set:

drop table dbo.testing
create table dbo.testing (retained int, dropped int)

insert into dbo.testing (retained, dropped)
select 972580 , 697688 union all
select 1354938, 697688 union all
select 1354938 , 972580 union all
select 1555243, 1354938

The result should be:



Any ideas?


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1363943
Posted Tuesday, September 25, 2012 6:25 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:47 PM
Points: 23,293, Visits: 32,028
Abu Dina (9/25/2012)
Lynne's solution works if we assume that the retained ID is always the maximum.

But tthe solution doesn't work with the following record set:

drop table dbo.testing 
create table dbo.testing (retained int, dropped int)

insert into dbo.testing (retained, dropped)
select 767884, 157441 union all
select 1046261, 157441 union all
select 6699, 157441 union all
select 157441, 73635 union all
select 767884, 73635 union all
select 1046261, 73635 union all
select 6699, 73635 union all
select 1046261, 767884 union all
select 6699, 767884 union all
select 6699, 1046261

I will keep trying to see if I can come up with a solution but if anyone else can think of something then that'd be great!

Thanks in advance.


I can only write code based on what you provided. Based on the sample data and expected results, what I saw was the max id being retained. With the new data, what are the rules for determining what ID is used? Also, you posted additional data but not additional expected results.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1363976
Posted Tuesday, September 25, 2012 6:34 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 3:26 AM
Points: 708, Visits: 3,287
True... Based on the original sample data it does look like it would work based on maximum id.

See my previous reply to ChrisM.

As I said, I have a working solution but I'm having to run my update several times until it works. Just wondering if there is an alternative solution which works with one pass.

Thanks for your efforts.


---------------------------------------------------------


It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
David Edwards - Media lens

Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
Howard Zinn
Post #1363990
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse