27,579
社区成员
发帖
与我相关
我的任务
分享
--又调整了一下,刚才有两个结果不对
declare @t table (col varchar(1))
insert into @t
select 'w' union all
select 'w' union all
select 'l' union all
select 'w' union all
select 'l' union all
select 'w' union all
select 'l' union all
select 'l' union all
select 'l' union all
select 'w' union all
select 'w'
create table #t(rid int,col varchar(2),num int)
insert into #t(rid,col)
select row_number() over (order by getdate()) as rid,* from @t
declare @i int set @i=1
update #t
set num=@i,@i=case when col='l' then @i+1 else 0 end
select a.col,
case when isnull(b.col,'w')='w' and a.col='w' then 200
when b.col='l' and a.col='w' then 200*power(2,b.num)
when a.col='l' then -200*power(2,b.num) end as col2
from #t a left join #t b on a.rid=b.rid+1
drop table #t
/*
col col2
---- -----------
w 200
w 200
l -200
w 400
l -200
w 400
l -200
l -400
l -800
w 1600
w 200
*/
declare @t table (col varchar(1))
insert into @t
select 'w' union all
select 'w' union all
select 'l' union all
select 'w' union all
select 'l' union all
select 'w' union all
select 'l' union all
select 'l' union all
select 'l' union all
select 'w' union all
select 'w'
create table #t(rid int,col varchar(2),num int)
insert into #t(rid,col)
select row_number() over (order by getdate()) as rid,* from @t
declare @i int set @i=1
update #t
set num=@i,@i=case when col='l' then @i+1 else 0 end
select a.col,
case when isnull(b.col,'w')='w' and a.col='w' then 200
when b.col='w' and a.col='l' then -200
when b.col='l' then 200*power(2,b.num) end as col2
from #t a left join #t b on a.rid=b.rid+1
drop table #t
/*
col col2
---- -----------
w 200
w 200
l -200
w 400
l -200
w 400
l -200
l 400
l 800
w 1600
w 200
*/
--关于次数问题我没有看明白
declare @t table (col varchar(1))
insert into @t
select 'w' union all
select 'w' union all
select 'l' union all
select 'w' union all
select 'l' union all
select 'w' union all
select 'l' union all
select 'l' union all
select 'l' union all
select 'w' union all
select 'w'
; with maco as
(
select row_number() over (order by getdate()) as rid,* from @t
)
select a.col,
case when isnull(b.col,'w')='w' and a.col='w' then 200
when b.col='w' and a.col='l' then -200 end as col2
from maco a
left join maco b on a.rid=b.rid+1
/*
col col2
---- -----------
w 200
w 200
l -200
w NULL
l -200
w NULL
l -200
l NULL
l NULL
w NULL
w 200
*/