日期:2014-05-16  浏览次数:20420 次

请教一个行变列的sql如何写
这个表的变化是否可以通过一条sql搞定? 列名不重要,主要是能否把第二列和第三的数值变成列?请教高手,谢谢。

原始表:
id ty num
1 200 10
1 201 11
1 202 80
1 203 20
2 201 21
2 203 18
3 201 20
3 202 10

结果表:
id ty1 ty2 ty3 ty4
1 10 11 80 20
2 21 0 18 0
3 20 10 0 0

数据我已经初始化:

select 1 [id],200 [ty],10 [num] into [#t]
union select 1 [id],201 [t],11 [num]
union select 1 [id],202 [t],80 [num]
union select 1 [id],203 [t],20 [num]
union select 2 [id],201 [t],21 [num]
union select 2 [id],203 [t],18 [num]
union select 3 [id],201 [t],20 [num]
union select 3 [id],202 [t],10 [num]


谢谢。只有27分。怎么能涨点呢?
------解决方案--------------------
楼主的数据库是2000,还是2005呢