question

David 2 1 avatar image
David 2 1 asked

How to retrieve data value from a previous row with select statement

ID DATE 1 2013-08-02 2 2013-07-04 3 2013-04-05 Hi there, How is it possible using TSQL 2000 to select a data value from a previous row by using a select statement based on a date value? For example, in the test table above if I query it and the date value is not equal then I want to receive the previous ID value. Therefore for 2013-07-04 I get the ID 2 and for 2013-08-01 I get 2 and for 2013-04-06 I get 3. Hope this makes sense, TIA.
sql-server-2000tsqlselect
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

What data type is the date field?
0 Likes 0 ·

1 Answer

· Write an Answer
Kev Riley avatar image
Kev Riley answered
[Assuming the datatypes - see my comment above] You can use `TOP` with a descending `order by` to get the one you want declare @YourTable table (id int, [date] datetime) insert into @YourTable select 1, '2013-08-02' insert into @YourTable select 2, '2013-07-04' insert into @YourTable select 3, '2013-04-05' declare @datetoquery datetime --query for 2013-07-04 set @datetoquery = '2013-07-04' select top 1 id, [date] from @YourTable where [date] ]]]]]]]
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thank you.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.