## Weird grouping problem thats driving my mad!

 Author Message Abu Dina SSCertifiable Group: General Forum Members Points: 7349 Visits: 3325 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 fooselect 43157, 31574 union allselect 43157, 35731 union allselect 51477, 35731 union allselect 65842, 31574 union allselect 65842, 43157 union allselect 77822, 35731 union allselect 77822, 43157 union allselect 77822, 49202 union allselect 79673, 31574 union allselect 79673, 43157 union allselect 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_resultselect 79673, 77822 union allselect 79673, 35731 union allselect 79673, 43157 union allselect 79673, 49202 union allselect 79673, 31574 union allselect 79673, 65842 union allselect 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 lensSociety 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 SSC Guru Group: General Forum Members Points: 225944 Visits: 40423 Can you explain the logic the takes the original data to the final results? Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Abu Dina SSCertifiable Group: General Forum Members Points: 7349 Visits: 3325 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 lensSociety 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 SSC Guru Group: General Forum Members Points: 225944 Visits: 40423 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. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) mister.magoo One Orange Chip Group: General Forum Members Points: 26706 Visits: 7939 I can't see why this is in the results?select 79673, 49202Can you explain? MM`select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);`Forum Etiquette: How to post Reporting Services problemsForum Etiquette: How to post data/code on a forum to get the best help - by Jeff ModenHow to Post Performance Problems - by Gail Shaw Lynn Pettis SSC Guru Group: General Forum Members Points: 225944 Visits: 40423 mister.magoo (5/12/2013)I can't see why this is in the results?select 79673, 49202Can you explain?I am too. Only thing I can come up with is indirect relationships. Makes it difficult to work with. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Bevan Keighley SSC Eights! Group: General Forum Members Points: 869 Visits: 857 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 lvlfrom #foo as awhere a.master_id = (select max(master_id) from #foo)union allselect 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 lvlfrom #foo as b inner join cte as c on c.duplicate_id = b.master_id or c.duplicate_id = b.duplicate_idand lvl<5)select distinct master_id, duplicate_id from ctewhere master_id <> duplicate_idOPTION (MAXRECURSION 50)`Regards,Bevan Keighley Abu Dina SSCertifiable Group: General Forum Members Points: 7349 Visits: 3325 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 lensSociety 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