I am using a Microsoft Access 2010 database to import values from one table and append them to a summary table.
One of the issues I am having is finding the previous and next value from the select statement.
This would look as follows.
JOINT JOINT AHEAD JOINT BEHIND
100103 200203
200203 300303 100103
300303 200203
I would like to create this using a SQL code
Be cautious when considering correlated subqueries. They can be very slow. And if you build a query which includes two correlated subqueries, you will magnify the problem.
If your source table contains a smallish number of rows (say a few dozen), the slowness may not be an issue. However, if the table includes a thousand rows you will most certainly notice it. And if your
JOINTfield is not indexed, the performance could be painfully slow.If you will be running your query from within an Access session, you can use domain functions (
DMinandDMax) instead of correlated subqueries. Domain functions are often criticized as slow. However, in this situation they can be dramatically faster than correlated subqueries.Correction: You don’t need to run your query from within an Access session for it to be able to use the
DMin()andDMax()functions. I attached a VBScript example which opens an ADO Recordset based on myqryDomainFunctions. It works without error and correctly reportsRecordCount: 1000I created a table,
joints, with a long integer fieldjointas primary key and added 1000 rows. Then I created these 2 queries:qryCorrelatedSubqueries:
qryDomainFunctions:
Here is a transcript from the Immediate window where I compared the speed of those 2 queries, using the
QueryDurationfunction below. That function returns duration in milliseconds.Note that both those queries benefit from the index on the
jointsfield. When I dropped the index, compacted the db, and re-ran the tests I got these results:This is the module with the code I used.
QueryDurationis by no means the last word on performance measurement. However it’s good enough to give us a rough idea of the relative speeds of those 2 queries.DomainFunctionsQuery.vbs: