Question

one is often opposed with a filled-in denormalized table, which is although having a primary key, such as:

| RowID  | Column1 | Column2 | Column3 | Column4 | ...
| 'Row1' | 'a1'    | 'a2'    | 'a3'    | 'a4'    |
| 'Row2' | 'b1'    | 'b2'    | 'b3'    | 'b4'    |
| 'Row3' | 'c1'    | 'c2'    | 'c3'    | 'c4'    |
...

Question is: how to efficiently convert it into the 3nf storing the data? (ideally, let's assume that the number of columns and their names are not known)

| RowID | ColumnID | Value |
| 1     | 1        | 'a1'  |
| 1     | 2        | 'a2'  |
...

or, ideally,

| RowID  | ColumnID  | Value |
| 'Row1' | 'Column1' | 'a1'  |
| 'Row1' | 'Column2' | 'a2'  |
...

One solution I'm aware of is selecting columns one under another with a couple of UNIONs, but the script is going to look terrible in case of many columns.

Any help is much appreciated!

Best regards, Constantine.

Was it helpful?

Solution

actually the "Unpivot" operator was what I needed.

Unpivot with column name

Best, Constantine

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top