基于SQL Server 2008中的序列/排名更新?

时间:2011-10-30 03:41:44

标签: sql-server-2008 rank

表格如下:

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为参数,我想知道我要求的checkvalue之前的请求step是否为空。

所以我想选择where step=20并获取NULL

我想选择where step=15并获得1

我试图想出一些基于“排名-1”的东西,但到目前为止还没有雪茄。

帮助?

1 个答案:

答案 0 :(得分:2)

declare @step int = 15

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