I was wondering if there exists code to accomplish the following in SQL-Server 2008?
Table 1:
id column name
-------------------
1 col1
2 col2
3 col3
4 col2
Table 2:
col1 col2 col3
--------------------
a b c
Result Table:
id data
--------------------
1 a
2 b
3 c
4 b
Thanks in advance, I really have no idea how to do this.
You can use
UNPIVOTtable2to access the data from the columns:see SQL Fiddle with Demo
Or you can use a
UNION ALLto access the data intable2:see SQL Fiddle with Demo