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


Consolidating records - TSQL problem


Consolidating records - TSQL problem

Author
Message
Abu Dina
Abu Dina
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5465 Visits: 3325
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)

Group: General Forum Members
Points: 167720 Visits: 39483
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;




Cool
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)
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96897 Visits: 21979
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. w00t


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

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)

Group: General Forum Members
Points: 167720 Visits: 39483
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. w00t


Not feeling well today? ;-)

Cool
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)
Abu Dina
Abu Dina
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5465 Visits: 3325
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
Abu Dina
Abu Dina
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5465 Visits: 3325
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
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)

Group: General Forum Members
Points: 73176 Visits: 20316
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
Abu Dina
Abu Dina
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5465 Visits: 3325
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 :-P


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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)SSC Guru (167K reputation)

Group: General Forum Members
Points: 167720 Visits: 39483
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.

Cool
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)
Abu Dina
Abu Dina
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5465 Visits: 3325
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
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