Solved

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

Posted on 2013-01-20
2
394 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

805 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