Search code examples
ms-accessms-access-2010ms-access-2013ms-access-2016

Query for Nearest Dates, along with respective values


I am fairly new to access and have taken the lynda online courses on queries, yet I still cannot figure out this problem. I currently have a query that returns something like the following information from 2 different relational tables. Access actually returns all possible combinations of date1 and date2 along with the associated values, but I've omitted the other results for the sake of brevity.

ID        date1         date2         Abs(date1-date2)   Date1 Lab Value
510010    12/09/2013    12/10/2013    1                  6
510010    10/09/2013    12/09/2013    60                 12
510010    11/09/2013    09/10/2013    61                 5
510010    03/09/2013    03/15/2013    6                  4
510012    06/09/2013    06/09/2013    0                  4
510012    03/25/2013    03/15/2013    10                 1

My goal is to have the query return unique record IDs with the smallest (minimum) difference between date 1 and date 2 (Abs(date1-date2)), along with all the respective field values (id, date1, date2, date1 lab value).

Expected Output:

ID        date1         date2         Abs(date1-date2)   Date1 Lab Value
510010    12/09/2013    12/10/2013    1                  6
510012    06/09/2013    06/09/2013    0                  4

This problem is very similar to this link; however I was wondering if the query can be done exclusively through design view using aggregate queries and the criteria field.

Thank you in advance!


Solution

  • Your post states that you want "unique record IDs", but unless you provide more details about the data or further selection criteria, it is still possible that June7's answer will return multiple rows for the same ID if more than one row has the same minimum date difference.

    I'll guess that June7's query is not working for you because "Dataset" in your case is a multi-table join. Instead, you need to first save your initial query and then apply June7's hint to the first query:

    Query1:

    SELECT Patients_t.ProstateID AS ID, PSA_t.PSA_Date AS [Date 1], MRI_t.MRI_Date AS [Date 2],
        PSA_t.PSa_Total AS [Date 1 Lab Data], Abs([PSA_date]-[MRI_Date]) AS Diff
    FROM (Patients_t INNER JOIN PSA_t ON Patients_t.PatientID = PSA_t.PatientID)
        INNER JOIN MRI_t ON Patients_t.PatientID = MRI_t.PatientID
    

    Query2:

    SELECT ID, [Date 1], [Date 2], [Date 1 Lab Data], Diff
    FROM Query1
    WHERE (Query1.Diff = DMin("[Diff]","Query1","ID=" & [ID]))
    

    The following alternative approach may be more efficient than using Domain Aggregate functions (i.e. DMin) and may be easier to apply other criteria in the case that you must absolutely limit the results to unique ID's as you stated. Don't let the multiple queries of this approach fool you into thinking that the Domain Aggregate functions are better since they look "shorter and cleaner". While use of DMin can be entered in the designer as you also requested, the following set of aggregate queries can also be created using only the designer. I of course post the SQL since it is easier to post than showing a bunch of Designer screenshots for each step.

    For example, to ensure uniqueness in the ID, suppose that we limit not only the difference between the dates, but then select the maximum [Date 2] when there are multiple matches to the first criteria. (But this still only assumes that [Date 2] dates will be unique which might not be true. In the event that NO data guarantees unique records, you can use grouping and the further aggregate functions (e.g. MIN, MAX, LAST, FIRST) on the final query to ensure unique IDs but you must take care that the selected Date 1 value still corresponds to the Date 1 Lab value.)

    Start with the same Query1:

    Query 3A - get minimum date differences:

    SELECT ID, Min(Query1.Diff) AS [Min Diff]
    FROM Query1
    GROUP BY Query1.ID
    

    Query 3B - apply original criteria AND get maximum Date 2 for each match:

    SELECT ID, [Min Diff], Max(Query1.[Date 2]) AS [Max Date 2]
    FROM Query3a INNER JOIN Query1
        ON (Query3a.ID = Query1.ID) AND (Query3a.[Min Diff] = Query1.Diff)
    GROUP BY ID, [Min Diff]
    

    Query 4 - Apply final selection:

    SELECT Query1.*
    FROM Query3B INNER JOIN Query1
        ON (Query3B.[Max Date 2] = Query1.[Date 2])
        AND (Query3B.[Min Diff] = Query1.Diff) AND (Query3B.ID = Query1.ID);