2017-03-01 88 views
-1

大家好,我想问你,如果我站在“ACCOUNTING MANAGER”上,我如何才能在sql server 2008中选择以前的记录,如下图所示我想选择“SELLER”如何在SQL Server 2008中选择以前的记录

enter image description here

+1

这是什么意思是你的意思? – Whencesoever

+2

什么是'5000-01-01'的TODATE' ......无论它确实有更好的方法来做任何代表。 – Tanner

回答

0

使用CTE喜欢使用ROW_NUMBER函数创建ROWNUMBER()....

with temp as (select *,row_number()over(order by [tooday] asc) as rn from tablename) 

select t1.jobDESC from temp t1 
join temp t2 on t1.rn =t2.rn-1 
where t2.jobDESC = 'ACCOUNTING MANAGER' 

变化为表名与表名..

0

这是将主键放在表中的更好方法。所以在你的表中创建一个主键。

select top 1 t1.* from table1 t1, table1 t2 
where t1.primaryKey = t2.primaryKey - 1 order by primaryKey desc 
0

你可以试试这个。首先你需要添加一列作为身份。

ALTER TABLE Your_TableName ADD AUTOID INT IDENTITY(1,1) 

然后您需要找到您现在正站在的记录(即“ACCOUNTING MANAGER”)的rowID。

declare @RowID INT 
Set @RowID=(Select AUTOID from Your_TableName where JOBDESC="ACCOUNTING MANAGER") 

然后

select * from Your_TableName where AUTOID=(@RowID-1) 

@RowID如果你想之前的纪录-1。

@ RowID + 1如果你想要下一个记录。