开发者

Help in Reverse Pivot Query

I have no idea how i am going to do this. Simple example

studentid    |   sub1id     | sub2id    | and so on.....
------------------------------------开发者_JAVA百科----------------------
1            |     1        |   2       | and so on
3            |     5        |   6       | and so on

Using such table structure... I want a output in the following form a particular student

Student 1

student    |  sub   |
======================
1          |   1    |
1          |   2    |

Student 2

student    |  sub   |
======================
2          |   5    |
2          |   6    |


declare @T table
(
  studentid int,
  sub1id int,
  sub2id int
)
insert into @T values
(1, 1, 2),
(3, 5, 6)

select studentid, sub
from (select studentid, sub1id, sub2id
      from @T
      where studentid = 3) as T
unpivot (sub for C in (sub1id, sub2id)) as U
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜