Skip to content
Advertisement

Pivot with changed structure of the final table

I have the following table –

How do I write a query to produce this output –

The output table is a connection table between all the columns in the input table. For example, if the value in column A <> column B in the input table then insert a record in the output table. If a value in column A <> column C, then insert another record. The reason rows 5, 6, 7 and 8 are omitted from the output is because those connections already exist in the output.

The reason 1124136593 is only connected to 2 values and not 3 is because 1053695668 is already connected to it in row 7 of the output.

Advertisement

Answer

I think you are describing an unpivot operation with the removal of duplicates in either direction:

The use of distinct on here prevents “reverse” duplicates. It also ensures that the resulting values have a in the first column.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement