I need to find a way to create a SQL view that allows me to transpose some data that in one table a particular column contains the column names from another table.
Table: FormControl:
| FormType | FieldName | ColumnName |
|---|---|---|
| STK | COL001 | Colour |
| STK | SIZ001 | Size1 |
| STK | LIC001 | License |
Table: FormData:
| StockCode | Colour | Size1 | License |
|---|---|---|---|
| 12345 | R | 2 | XX |
| ABCDE | L | 1 | AA |
| AB123 | G | 3 | BB |
Required resulting data from view:
| FormType | StockCode | FieldName | Value |
|---|---|---|---|
| STK | 12345 | COL001 | R |
| STK | 12345 | SIZ001 | 2 |
| STK | 12345 | LIC001 | XX |
| STK | ABCDE | COL001 | L |
| STK | ABCDE | SIZ001 | 1 |
| STK | ABCDE | LIC001 | AA |
| STK | AB123 | COL001 | G |
| STK | AB123 | SIZ001 | 3 |
| STK | AB123 | LIC001 | BB |
The field names/columns might change, so ideally I need the view to be dynamic but, if that's not possible, a fixed one with just the current fields/columns in is fine for now.
Any ideas please?
Have tried using PIVOT but have got a bit lost in how to do it.