sql一般查询得到的结果是
字段1 字段2 字段3 字段4 字段5
值1_A 值2_A 值3_A 值4_A 值5_A
值1_B 值2_B 值3_B 值4_B 值5_B
值1_C 值2_C 值3_C 值4_C 值5_C
值1_D 值2_D 值3_D 值4_D 值5_D
能不能把查询结果变为
上方可以自己命名新的名字
字段1 值1_A 值1_B 值1_C 值1_D
字段2 值2_A 值2_B 值2_C 值2_D
字段3 值3_A 值3_B 值3_C 值3_D
字段4 值4_A 值4_B 值4_C 值4_D
字段5 值5_A 值5_B 值5_C 值5_D
这能不能算是特殊的行转列
------解决方案--------------------
去搜索90°旋转的例子 N多。
------解决方案--------------------
/*
将表数据旋转90度(2007-11-19于海南三亚)
将下表数据:
A b c d e
-------------------- ----------- ----------- ----------- -----------
x 1 2 3 4
y 5 6 7 8
z 9 10 11 12
转化成如下结果:
a x y z
-------------------- ---------- ---------- ----------
b 1 5 9
c 2 6 10
d 3 7 11
e 4 8 12
*/
--生成测试数据
create table test1(A varchar(20),b int,c int,d int,e int)
insert into test1 select 'x',1,2 ,3 ,4
insert into test1 select 'y',5,6 ,7 ,8
insert into test1 select 'z',9,10,11,12
go
--生成中间数据表
declare @s varchar(8000)
set @s = 'create table test2(a varchar(20)'
select @s = @s + ',' + A + ' varchar(10)' from test1
set @s = @s + ')'
exec(@s)
print @s
--借助中间表实现行列转换
declare @name varchar(20)
declare t_cursor cursor for
select name from syscolumns
where id=object_id('test1') and colid > 1 order by colid