一张表的查询
create table test0518
(aID int primary key identity,
a varchar(10)
)
go
insert into test0518 (a) values ( 'x ')
insert into test0518 (a) values ( 'y ')
insert into test0518 (a) values ( 'z ')
insert into test0518 (a) values ( 'x ')
insert into test0518 (a) values ( 'y ')
insert into test0518 (a) values ( 'y ')
insert into test0518 (a) values ( 'z ')
原表
aID a
1 x
2 y
3 z
4 x
5 y
6 y
7 z
如何把x下面到下一个z之间的都填上x
如何把一下面到下一个z之间的都填上y
如何把一下面到下一个z之间的都填上z
结果
a b c d
x x
y x y
z x y z
x x
y x y
y x y
z x y z
------解决方案--------------------这样吗?
select a,
case when a between 'x ' and 'z ' then 'x ' else ' ' end as b,
case when a> 'x ' and a <= 'z ' then 'y ' else ' ' end as c,
case when a= 'z ' then 'z ' else ' ' end as d
from test0518
------解决方案--------------------楼上的就可以实现
------解决方案-------------------- select aid,a ,(case when aid <= (select top 1 aid from test0518 b where b.a= 'z ' and a.a> = 'x ' and b.aid-a.aid> =0) then b end),
(case when aid <= (select top 1 aid from test0518 b where b.a= 'z ' and a.a> = 'y ' and b.aid-a.aid> =0) then b end),
(case when aid <= (select top 1 aid from test0518 b where b.a= 'z ' and a.a> = 'z ' and b.aid-a.aid> =0) then