I have one single table and in that table there are three columns company1, company2, company3.
Below is my table data :
Company1 Company2 Company3
ABC
Test1 Test3 Test5
Test2 Test4 Test6
testing testing2
And I want to combine these columns into single column with serial number like below :
SrNo CompanyName
1 ABC
2 Test1
3 Test2
4 testing
5 Test3
6 Test4
7 testing2
8 Test5
9 Test6
Best Answer
One way to get the desired result is with
UNION ALL
of aSELECT
query for each company column. Assign the serial number value in order by source column and company name usingROW_NUMBER
in the outerSELECT
.