I apologize in advance if this question has been asked before.
I have two tables. Table one has three columns CustomerID, SequenceNum, Value
, table two has a large amount of columns. I would like to fill in the columns of table two with the values of table one by column, not by row.
An example:
------------------------------------
| CustomerID | SequenceNum | Value |
------------------------------------
| 1 | 1 | A |
------------------------------------
| 1 | 2 | B |
------------------------------------
| 1 | 3 | C |
------------------------------------
| 2 | 1 | Q |
------------------------------------
| 2 | 2 | R |
------------------------------------
| 3 | 1 | X |
------------------------------------
becomes
---------------------------------------------------------------------
| CustomerID | PrimaryVal | OtherVal1 | OtherVal2 | OtherVal3 | ... |
---------------------------------------------------------------------
| 1 | A | B | C | NULL | ... |
---------------------------------------------------------------------
| 2 | Q | R | NULL | NULL | ... |
----------------------------------------------------------------------
| 3 | X | NULL | NULL | NULL | ... |
---------------------------------------------------------------------
In essence. Each unique CustomerID
in table one will have a single row in table two. Each SequenceNum
of a particular CustomerID
will fill in a column in table two under PrimaryVal, OtherVal1, OtherVal2, etc.
. A row which has a SequenceNum
equal to 1 will fill the PrimaryVal
field, and 2-18 (the maximum sequence length is 18) will fill OtherVal#
.
The main problem I see is the variable amount of values in a sequence. Some sequences may only contain 1 row, some will fill up all 18 spots, and anything in between.
Any advice on how to solve this problem would be greatly appreciated. Thank you.