create cursor triplets ( key c(1) ) create cursor tripletpairs ( key c(1), pair c(3)) InsertData( 'a','1,2','1,3','2,3' ) InsertData( 'b','1,2','1,4','2,4' ) InsertData( 'c','1,2','1,5','2,5' ) InsertData( 'd','1,3','1,4','3,4' ) InsertData( 'e','1,3','1,5','3,5' ) InsertData( 'f','1,4','1,5','4,5' ) InsertData( 'g','2,3','2,4','3,4' ) InsertData( 'h','2,3','2,5','3,5' ) InsertData( 'i','2,4','2,5','4,5' ) InsertData( 'j','3,4','3,5','4,5' ) function InsertData( pcKey, pcVal1, pcVal2, pcVal3 ) insert into triplets values ( pcKey ) insert into tripletpairs values ( pcKey, pcVal1 ) insert into tripletpairs values ( pcKey, pcVal2 ) insert into tripletpairs values ( pcKey, pcVal3 ) returnto test the 4 triplets for a solution using code similar to what I posted earlier:
select triplets.key as key1, triplets2.key as key2, triplets3.key as key3, triplets4.key as key4 ; from triplets ; left join triplets as triplets2 on triplets.key < triplets2.key ; left join triplets as triplets3 on triplets.key < triplets3.key and ; triplets2.key < triplets3.key ; left join triplets as triplets4 on triplets.key < triplets4.key and ; triplets2.key < triplets4.key and ; triplets3.key < triplets4.key ; into cursor TheCombinations ; having ! isnull( key2 ) and ! isnull( key3 ) and ! isnull( key4 )You now have a cursor of all possible combinations of your triplets taken 4 at a time. Each one needs to be tested as a solution. It is a solution if it covers all 10 of the original pairs.
select TheCombinations scan scatter name thiscombination select distinct tripletpairs.pair ; from tripletpairs ; into cursor testsolution ; where key = thiscombination.key1 or ; key = thiscombination.key2 or ; key = thiscombination.key3 or ; key = thiscombination.key4 if _tally = 10 * it's a solution ? thiscombination.key1, thiscombination.key2, thiscombination.key3, thiscombination.key4 endif endscanI assume the first solution is sufficient so you could exit the loop. Your sample data has 10 solutions. The code can also be made more dynamic with macros to test the 5,6,7,8,9 and 10 triplet combinations if required.