I have a table of observations. This table is loaded as a recordset, and processed to be used in a Treeview control.
Part of this process is to determine which icon to display for the record in the treeview. The icon is determined by the status, and the status is determined by the latest related record in tbl_Updates.
Table Date:
tbl_Obs
PK_Obs (Autonumber)
tx_Obs (Text)
... (other fields)
tbl_Status
PK_Status (Autonumber)
tx_Status (Text)
tbl_Updates
PK_Update (Autonumber)
ID_Obs (Foreign key to PK_Obs, Long)
tx_Update
ID_Status (Foreign key to PK_Status)
dt_Date (Date/Time Timestamp of record creation)
... (other fields)
The Program flow:
A observation is created into tbl_Obs. At various points during the lifetime of a observation it will get updated and a new record is created in the update table each time. Update records are new er deleted or overwritten.
An example could be:
Observation (1):
Alignment of tracks is off by 2 cm
Updates:
2012-11-10 Open - Documentation Requested from supplier
2012-11-11 Open - Documentation Received from supplier
2012-11-13 Closed - Documentation evaluated and found satisfying.
The status of the observation is always determined by the latest update. So for the point of loading my information into the treeview I need a query giving me the PK_Obs, tx_Obs and the LATEST (based on date) ID_Status from the observations table.
Sure I can get this done by the use of multiple queries or subqueries, but I wonder what what would be the most efficient way to retrieve this? And why exactly is it the most efficient way?
The (a) subquery way:
Creating an extra query:
I could create an extra query with the syntax:
And then join this to my main query.
Part of this process is to determine which icon to display for the record in the treeview. The icon is determined by the status, and the status is determined by the latest related record in tbl_Updates.
Table Date:
tbl_Obs
PK_Obs (Autonumber)
tx_Obs (Text)
... (other fields)
tbl_Status
PK_Status (Autonumber)
tx_Status (Text)
tbl_Updates
PK_Update (Autonumber)
ID_Obs (Foreign key to PK_Obs, Long)
tx_Update
ID_Status (Foreign key to PK_Status)
dt_Date (Date/Time Timestamp of record creation)
... (other fields)
The Program flow:
A observation is created into tbl_Obs. At various points during the lifetime of a observation it will get updated and a new record is created in the update table each time. Update records are new er deleted or overwritten.
An example could be:
Observation (1):
Alignment of tracks is off by 2 cm
Updates:
2012-11-10 Open - Documentation Requested from supplier
2012-11-11 Open - Documentation Received from supplier
2012-11-13 Closed - Documentation evaluated and found satisfying.
The status of the observation is always determined by the latest update. So for the point of loading my information into the treeview I need a query giving me the PK_Obs, tx_Obs and the LATEST (based on date) ID_Status from the observations table.
Sure I can get this done by the use of multiple queries or subqueries, but I wonder what what would be the most efficient way to retrieve this? And why exactly is it the most efficient way?
The (a) subquery way:
Code:
SELECT tbl_Obs.PK_Observation,
tbl_Obs.mem_Observation,
(SELECT TOP 1 U.ID_Status
from tbl_ObsUpdate AS U
WHERE U.ID_Obs=PK_Observation
ORDER BY U.dt_Created DESC) AS Status
FROM tbl_Obs
Creating an extra query:
I could create an extra query with the syntax:
Code:
SELECT tbl_ObsUpdate.ID_Obs,
Last(tbl_ObsUpdate.ID_Status) AS LastOfID_Status
FROM tbl_ObsUpdate
GROUP BY tbl_ObsUpdate.ID_Obs
ORDER BY Max(tbl_ObsUpdate.dt_Created) DESC;
Comment