'How can I unpivot column values [duplicate]

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source