I need to unpivot columns to rows and then have the distinct values alongside. Ideally using postgresql but could use python if solution is easier. Also needs to be dynamic as the fields will vary each time the transformation is run. I've trawled through the search but struggling to find anything that resembles this.
Source data table:
| option1 | option2 | option3 |
|---|---|---|
| 1 | A | X |
| 1 | B | Y |
| 2 | A | X |
| 3 | B | Y |
| 3 | A | X |
Target table:
| fieldname | option |
|---|---|
| option1 | 1 |
| option1 | 2 |
| option1 | 3 |
| option2 | A |
| option2 | B |
| option3 | X |
| option3 | Y |