I need to get a table that has every permutation of items that appear in either column A or B of another table. That's not sounding clear, so here's sample DDL/DML:create table #tSource (item_id1 int,item_id2 int,random_unused int)insert into #tSource(item_id1,item_id2) VALUES (1,2,55)insert into #tSource(item_id1,item_id2) VALUES (2,5,66)insert into #tSource(item_id1,item_id2) VALUES (3,1,77)insert into #tSource(item_id1,item_id2) VALUES (4,9,88)create table #tPermutations (item_id1 int, item_id2 int)insert into #tPermutations (item_id1,item_id2) select (your magic here)
In the above example, the result I'd be looking for is:item_id1 item_id2-------- --------1 21 31 41 51 92 12 32 42 5 2 93 13 23 43 53 94 14 24 34 54 95 15 25 35 45 99 19 29 39 49 5
That is, every item that appears in either item_id1 or item_id2 of #tSource will have every permutation of itself and every other item that appears in item_id1 or item_id2 of #tSource. There are other columns, represented here by random_unused, which should be ignored.I'm stumped. I see how to do it with a bunch of intermediate steps and select distincts, but it seems like there's got to be a better way.Cheers-b