首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么把表格横向排列

2012-02-05 
如何把表格横向排列?如下:aidps0112029037bidps10420819073cidps2110024135希望得到如下结果idps011204 21

如何把表格横向排列?
如下:
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 rowidAlter table a drop column rowidAlter table a drop column rowiddrop table adrop table bdrop table c
[解决办法]
不好意思,刚才没有看清楚,我得出来的结果跟楼主想要的好象有点出入。。。
因为楼主没有id列。所以需要加个id列来区分。。
我重新改了下
SQL code
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 

热点排行
Bad Request.