2011-10-30 60 views
1

表看起來是這樣的:根據SQL Server 2008中的序列/等級進行更新?

create table #rankme (rankmeid int identity(1000, 1) primary key, 
         step int null, checkvalue int null) 

insert into #rankme values (10 , 1) 
insert into #rankme values (15 , null) 
insert into #rankme values (20 , null) 
insert into #rankme values (40 , null) 

select * from #rankme order by step,checkvalue 

step作爲參數,我試圖找出是否要求checkvaluestep前一次我問是空的。

所以我想選擇where step=20並得到NULL。我想選擇where step=15並獲得1

我試圖想出一些基於「等級1」但迄今沒有雪茄的東西。

幫助?

回答

2
declare @step int = 15 

select top(1) R.checkvalue 
from #rankme as R 
where R.step < @step 
order by R.step desc