I am trying to get the column position of a value while doing an unpivot on the columns col2, col3, col4 in SQL server
df1
col1 col2 col3 col4
1 AAA BBB
2 ABC CCC DDD
result
col1 [All] [position]
1 AAA 1
1 BBB 2
2 ABC 1
2 CCC 2
2 DDD 3
I am able to unpivot the table by using the following
SELECT a.col1, a.[all]
from df1 as t
UNPIVOT
(
[all] for col_nm in (
col2, col3, col4
) as a