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

SQL数据库列行转换?
是要在一个表中的,表结构如下:
ID A1 B1 A2 B2
1 a1 b1 a2 b2
2 a10 b10 a20 b20
转换成
ID A1 B1
1 a1 b1
2 a10 b10
3 a2 b2
4 a20 b20
可以这样转换吗?

------解决方案--------------------
SQL code
select A1,B1 from tbname
union all
select A2,B2 from tbname

------解决方案--------------------
SQL code
--Code
select row_number() over(order by A1) as id,t.* from
(
select A1,B1 from @T
union all
select A2,B2 from @T
) t
--Drop

--Result
/*
id                   A1   B1
-------------------- ---- ----
1                    a1   b1
2                    a10  b10
3                    a2   b2
4                    a20  b20
*/