如何把表格横向排列?
如下:
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
[解决办法]
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
Alter table a drop column rowidAlter table a drop column rowidAlter table a drop column rowiddrop table adrop table bdrop table c
[解决办法]
不好意思,刚才没有看清楚,我得出来的结果跟楼主想要的好象有点出入。。。
因为楼主没有id列。所以需要加个id列来区分。。
我重新改了下
create table a(id varchar(10),ps int)insert into a select '01',12insert into a select '02',9insert into a select '03',7create table b(id varchar(10),ps1 int)insert into b select '04',2insert into b select '08',19insert into b select '07',3create table c(id varchar(10),ps2 int)insert into c select '11',0insert into c select '02',4insert into c select '13',5select pk=identity(int,1,1),id ,ps into a1 from aselect pk1=identity(int,1,1),id ,ps1 into b1 from bselect pk2=identity(int,1,1),id ,ps2 into c1 from cselect 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)pwhere bh=bh1 and bh1=bh2