I'm looking to stack two columns into one. I know this can be done with a Union. But the bit I'm struggling with is I need to still identify which column the figure originated. Example below:
from:
| A | B | C |
| ------------ | ------------ | ------------ |
| X | 1 | 2 |
| Y | 3 | 4 |
| Z | 5 | 6 |
to:
A | new |
---|---|
X_B | 1 |
Y_B | 3 |
Z_B | 5 |
X_C | 2 |
Y_C | 4 |
Z_C | 6 |
3条答案
按热度按时间km0tfn4u1#
This is a quite strange requirement. I would suggest this type of odd manipulation is probably better left to the front end.
But the query is pretty straight forward. This will produce the output you are looking for.
4ioopgfo2#
Something like this mayhaps:
With help of
CROSS APPLY (VALUES
, you explode your rows into two rows, one for B and one for Cgtlvzcf83#
You can use the
UNPIVOT()
function for this, e.g.