문제

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.

도움이 되었습니까?

해결책

actually the "Unpivot" operator was what I needed.

Unpivot with column name

Best, Constantine

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top