Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to get 1 row previous from Current Row

Author  Topic 

khufiamalik
Posting Yak Master

120 Posts

Posted - 2008-09-10 : 00:07:39
Hello All,
I have a query which gives me multiple rows result.

and I have a field Status which will be 'T' for only one record in the whole result set.
I just want to get 1 previous row from Status=T

Sample Data
ID Amount Status
1 1000 N
2 500 N
3 700 T
4 800 N
5 500 N


Here my requirement is to fetch 1 previous record Where Status=T
means I want to get where Amount=500 and ID=2

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-10 : 00:09:37
is the ID in running sequence ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khufiamalik
Posting Yak Master

120 Posts

Posted - 2008-09-10 : 00:12:22
No I don't know the ID on the time of query and it can be manupulated by user , it is not auto generated.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-09-10 : 00:17:28
[code]select top 1 *
from SampleData
where ID < (select ID from SampleData where Status = 'T')
order by ID desc[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -