May 12, 2013 at 12:30 pm
Can somone help me with the following grouping problem?
I have a process which produced a table like the below:
create table foo(master_id int, duplicate_id int)
insert into foo
select 43157,31574 union all
select 43157,35731 union all
select 51477,35731 union all
select 65842,31574 union all
select 65842,43157 union all
select 77822,35731 union all
select 77822,43157 union all
select 77822,49202 union all
select 79673,31574 union all
select 79673,43157 union all
select 79673,65842
I would like to change the result to the below:
create table foo_result (master_id int, duplicate_id int)
insert into foo_result
select 79673, 77822 union all
select 79673, 35731 union all
select 79673, 43157 union all
select 79673, 49202 union all
select 79673, 31574 union all
select 79673, 65842 union all
select 79673, 51477
Any suggestions please?
---------------------------------------------------------
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[/url]
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
May 12, 2013 at 2:07 pm
Can you explain the logic the takes the original data to the final results?
May 12, 2013 at 3:02 pm
The original table is the product of a record linkage process. All the ids are linked together. I know it's hard to spot but if you go through each row you can see how they all join together.
To simplify the processing of the next stage I would like to pick the maximum I'd and and link all other records to it.
Hope this makes sense.
---------------------------------------------------------
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[/url]
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
May 12, 2013 at 3:10 pm
I saw they were linked, I wanted to know the logic that generated your final list. Thank you, now I have something to work with here.
May 12, 2013 at 3:58 pm
I can't see why this is in the results?
select 79673, 49202
Can you explain?
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
May 12, 2013 at 4:04 pm
mister.magoo (5/12/2013)
I can't see why this is in the results?select 79673, 49202
Can you explain?
I am too. Only thing I can come up with is indirect relationships. Makes it difficult to work with.
May 12, 2013 at 4:34 pm
Hi,
Let us know if this is the sort of thing you are after. Note that I have recursively joined back to the duplicate_id and the master_id. Hope that is how they should join.
with cte(master_id , duplicate_id, lvl)
as
(
select a.master_id, duplicate_id, 1 as lvl
from #foo as a
where a.master_id = (select max(master_id) from #foo)
union all
select c.master_id as master_id, case when c.duplicate_id = b.master_id then b.duplicate_id when c.duplicate_id = b.duplicate_id then b.master_id else null end as duplicate_id, c.lvl +1 as lvl
from #foo as b
inner join cte as c
on c.duplicate_id = b.master_id or c.duplicate_id = b.duplicate_id
and lvl<5
)
select distinct master_id, duplicate_id from cte
where master_id <> duplicate_id
OPTION (MAXRECURSION 50)
Regards,
Bevan Keighley
May 12, 2013 at 5:03 pm
Yes, there are a number of indirect relationships and it gave me a lot of headaches.
It's just gone past midnight here in the uk so I'm off to bed. Will try out the solution suggested first thing tomorrow morning.
Cheers.
---------------------------------------------------------
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[/url]
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
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply