0

I have this table has shown bellow

Code CodP_1 CodP_2 CodP_3 ... Quant_1 Quant_2 Quant_3 ....
124 apple bananas orange ... 2 9 3 ...
248 peach NULL NULL ... 4 0 0 ...

The desired result:

Code CodP Quantity
124 apple 2
124 bananas 9
124 orange 3
248 peach 4

I have found some similar problems regarding this, and I have tried to use the unpivot. But my main challenge is that the codP has 40 columns (and I have to use all of them) and the quantity column, that it is related with the CodP has you've seen on the example, also has 40 columns, and I need to use all of them.

igdc
  • 25
  • 7
  • Then you need to (unfortunately) list all 80 of those columns; hopefully the reason you're going through this exercise it to normalise said design so you don't have to in the future. – Larnu Apr 04 '22 at 13:54
  • @Larnu yes it is :/ – igdc Apr 04 '22 at 13:56
  • 1
    To unpivot pairs of columns use [CROSS APPLY ... VALUES](https://stackoverflow.com/a/9275865/73226) rather than UNPIVOT – Martin Smith Apr 04 '22 at 14:03

0 Answers0