日期:2014-05-18  浏览次数:20677 次

如何把行转换成列(求助高手帮忙,给高分)
例如:
一条记录:
pid   ZeroP1   ZeroP2,ZeroP3,OneP1,OneP2,OneP3,TwoP1,TwoP2,TwoP3,ThreeP1,ThreeP2,ThreeP3,FourP1,FourP2,FourP3


现在要求把这条记录转化为如下三条记录:
pid       ZeroP1         ZeroP2             ZeroP3
pid       OneP1           OneP2               OneP3
pid       TwoP1           TwoP2               TwoP3
pid       ThreeP1       ThreeP2           ThreeP3
pid       FourP1         FourP2             FourP3



------解决方案--------------------
select pid, ZeroP1, ZeroP2,ZeroP3 from 表名
union all
select pid, OneP1,OneP2,OneP3 from 表名
union all
select pid, TwoP1,TwoP2,TwoP3 from 表名
union all
select pid, ThreeP1,ThreeP2,ThreeP3 from 表名
union all
select pid, FourP1,FourP2,FourP3 from 表名

------解决方案--------------------
select * from (
select pid, ZeroP1, ZeroP2,ZeroP3 from 表名
union all
select pid, OneP1,OneP2,OneP3 from 表名
union all
select pid, TwoP1,TwoP2,TwoP3 from 表名
union all
select pid, ThreeP1,ThreeP2,ThreeP3 from 表名
union all
select pid, FourP1,FourP2,FourP3 from 表名) a order by pid
------解决方案--------------------
上面都对
------解决方案--------------------
union all

将两个或更多查询的结果组合为单个结果集,
该结果集包含联合查询中的所有查询的全部行