Col1 Col2 Col3 Col4
1 A B C
2 E F G
3 I J K
4 M N O
And need a query to get the output as below;
Col1 MergedCol
1 A
1 B
1 C
2 E
2 F
2 G
3 I
3 J
3 K
4 M
4 N
4 O
And I have a simple strait forward query to get this result.
SELECT col1,col2 from MyTable
Union ALL
SELECT col1,col3 from MyTable
Union ALL
SELECT col1,col4 from MyTable
Union ALL
SELECT col1,col5 from MyTable
Ans:
Create Table TblTest
(
Col1 Int,
Col2 Varchar(50),
Col3 Varchar(50),
Col4 Varchar(50),
)
Insert Into TblTest Values (1,'A','B','C'),(2,'E','F','G'),(3,'I','J','K'),(4,'M','N','O')
Select * From TblTest
Select Col1,Upt.ColNew From TblTest UnPivot (ColNew For ColNewSt In (Col2,Col3,Col4))AS UPT
No comments:
Post a Comment