Solved

Access - Query to get Previous Active Record for Each next record.......

Posted on 2013-01-20
2
400 Views
Last Modified: 2013-01-20
Hello all
I need a query that for each record I have a field that indicates what the last active record number was.

Let's say I have a table called [test1] that has fields:

[TestID]- autonumber
[Control] - long
[Voided] - Y/N

Let's say my records are:
TestID   -  Control  - 0/-1
1 ------------ 10 ------------ 0
2 ------------ 16 ------------ 0
3 ------------ 19 ------------ -1
4 ------------ 20 ------------ 0
5 ------------ 30 ------------ -1
6 ------------ 31 ------------ 0
7 ------------ 38 ------------ 0
8 ------------ 41 ------------ 0
9 ------------ 44 ------------ -1
10 ------------ 45 ------------ 0

So then the Query Result that I would want is:
TestID   -  Control  - PreviousTestID
1 ------------ 10 ------------ Null
2 ------------ 16 ------------ 1
4 ------------ 20 ------------ 2
6 ------------ 31 ------------  4
7 ------------ 38 ------------ 6
8 ------------ 41 ------------  7
10 ------------ 45 ------------- 8
0
Comment
Question by:wlwebb
2 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 38798292
Try...

Select T.*,  (select max(testID) from test1 where Voided = false and TestId<T.TestID) as PrevId
from test1 as T
where Voided = false
0
 

Author Closing Comment

by:wlwebb
ID: 38798316
Perfect........  Thank you!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question