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

怎么样分栏显示并排序?请帮忙!
BM               MC              
0001         A单位
0002         A1单位
0003         B单位
0004         A2单位
0005         B1单位
0006         C单位

要求分四列显示,并按MC排序(结果要求如下:)

0001       A单位                 0002       A1单位
0004       A2单位               0003       B单位
0005       B1单位               0006       C单位

------解决方案--------------------
select a.BM a,a.MC b,b.BM c,b.MC d from 表 a left join 表 b on rtrim(a.BM)=rtrim(b.bm)-1 and (a.bm%2)=1
------解决方案--------------------

create table tb(bm varchar(10),mc varchar(20))
insert into tb
select '0001 ', 'A单位 ' union all
select '0002 ', 'A1单位 ' union all
select '0003 ', 'B单位 ' union all
select '0004 ', 'A2单位 ' union all
select '0005 ', 'B1单位 ' union all
select '0006 ', 'C单位 '
select identity(int,1,1) id,* into # from tb order by mc
declare @t table(id int,bm varchar(10),mc varchar(20),bm2 varchar(10),mc2 varchar(20))
insert into @t(id,bm,mc)
select * from # where id%2=1
update b set bm2=a.bm,mc2=a.mc from @t b, # a where b.id=a.id-1
select bm,mc,bm2,mc2 from @t

drop table tb,#
------解决方案--------------------
create table tb(bm varchar(10),mc varchar(20))
insert into tb
select '0001 ', 'A单位 ' union all
select '0002 ', 'A1单位 ' union all
select '0003 ', 'B单位 ' union all
select '0004 ', 'A2单位 ' union all
select '0005 ', 'B1单位 ' union all
select '0006 ', 'C单位 '


select a.BM a,a.MC b,b.BM c,b.MC d
from tb a
left join tb b
on rtrim(a.BM)=rtrim(b.bm)-1
where (a.bm%2)=1
/*

a b c d
---------- -------------------- ---------- --------------------
0001 A单位 0002 A1单位
0003 B单位 0004 A2单位
0005 B1单位 0006 C单位

*/

drop table tb