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

如何把表格横向排列?
如下:
a
id ps
01 12
02 9
03 7

b
id ps1
04 2
08 19
07 3

c
id ps2
11 0
02 4
13 5

希望得到如下结果
id ps  
01 12 04 2 11 0
02 9 08 19 02 4 
03 7 07 3 13 5

------解决方案--------------------
select id1=identity(int,1,1) , * into tmpa from a
select id1=identity(int,1,1) , * into tmpb from b
select id1=identity(int,1,1) , * into tmpc from c
select tmpa.id , tmpa.ps , tmpb.id , tmpb.ps , tmpc.id , tmpc.ps
from tmpa,tmpb,tmpc
where tmpa.id = tmpb.id and tmpa.id = tmpc.id

------解决方案--------------------
SQL code
create table a 
(id varchar(5), ps int)
create table b
(id varchar(5), ps1 int)
create table c
(id varchar(5), ps2 int)

insert a
select '01', 12
union all select '02', 9
union all select '03', 7

insert b
select '04', 2
union all select '08', 19
union all select '07', 3

insert c
select '11', 0
union all select '02', 4
union all select '13', 5

Alter table a add rowid int identity(1,1)
Alter table b add rowid int identity(1,1)
Alter table c add rowid int identity(1,1)

select a.id, a.ps, b.id, b.ps1, c.id, c.ps2
from a JOIN b ON a.rowid = b.rowid
JOIN c ON a.rowid = c.rowid


>>>
id ps id ps1 id ps2
----- ----------- ----- ----------- ----- -----------
01 12 04 2 11 0
02 9 08 19 02 4
03 7 07 3 13 5
SQL code

Alter table a drop column rowid
Alter table a drop column rowid
Alter table a drop column rowid

drop table a
drop table b
drop table c

------解决方案--------------------
不好意思,刚才没有看清楚,我得出来的结果跟楼主想要的好象有点出入。。。
因为楼主没有id列。所以需要加个id列来区分。。
我重新改了下
SQL code


create table a(id varchar(10),ps int)
insert into a select '01',12
insert into a select '02',9
insert into a select '03',7

create table b(id varchar(10),ps1 int)
insert into b select '04',2
insert into b select '08',19
insert into b select '07',3

create table c(id varchar(10),ps2 int)
insert into c select '11',0
insert into c select '02',4
insert into c select '13',5

select pk=identity(int,1,1),id ,ps into a1 from a
select pk1=identity(int,1,1),id ,ps1 into b1 from b
select pk2=identity(int,1,1),id ,ps2 into c1 from c
select id,ps,id1[ ],ps1 [ ],id2 [ ],ps2[ ] from
(select id,ps,bh=(select count(1) from a1 where pk<o.pk)+1 from a1 o)k,
(select id [id1],ps1,bh1=(select count(1) from b1 where pk1<m.pk1)+1 from b1 m)l,
(select id [id2],ps2,bh2=(select count(1) from c1 where pk2<n.pk2)+1 from c1 n)p
where bh=bh1 and bh1=bh2