ms-access


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!

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);

Here is one approach:
SELECT ID, Date1, Date2, Date1_LabValue, Abs([Date1]-[Date2]) AS Diff
FROM Dataset
WHERE (((Abs([Date1]-[Date2]))=DMin("Abs([Date1]-[Date2])","Dataset","ID=" & [ID])));
"Dataset" is whatever table or query with the data shown in your example, except for the date difference calculated field. I calc that here.


Related Links

Long Text change to rich text still only allows 255 characters in ACCESS 2016 db
Use variable to SELECT records in a recordset
Microsoft Access 2010: ComboBox text extraction / search
“Couldn't find Instalable ISAM” VB6 Data Control to Access DB
Replace Keywords
Access Navigation Control BrowseTo "The command or action 'BrowseTo' isn't available now. Error 2046
Add a record (that does not already exist) to a table from a userform
Access 2010 report invoice total
linked table xxx is unavailable. Microsoft access cannot contact the server
Does this Dlookup have incorrect syntax?
How to combine two fields in Form to one field from Microsoft Access?
Report building from selection in datasheet
Multiple arguments IsNull function MS Access VBA
how to pass parameters to ExportWithFormatting using vbscript
Access 2010 cascading combo boxes runtime error 424
Access Month Calendar View

Categories

HOME
deployment
azure-data-factory
react-router
autotools
primary-key
google-cloud-ml
size
dynamics-crm-online
ghc
conemu
functional-testing
graphicsmagick
tapestry
google-search-api
mustache.php
dxf
react-css-modules
info.plist
uninstall
typo3-6.2.x
subdomains
google-cloud-nl
ecto
column-family
code-contracts
swift3.0.2
event-driven
theano.scan
awt
unoconv
sql-server-agent
password-encryption
arena-simulation
midl
import-from-excel
galleria
avconv
blogengine.net
pg-dump
spring-cache
wdf
ado.net-entity-data-model
login-control
disque
two-factor-authentication
lemon
citrus-pay
coveralls
splash
typekit
camanjs
applescript-objc
sgen
rebol3
marmalade
datagridviewcolumn
android-2.2-froyo
resty-gwt
imdbpy
floating-point-precision
spring-io
quartz-graphics
pygit2
bitsharp
first-responder
frameset
word-processor
appconkit
uiviewanimation-curve
reddot
cufon
subscript
gnustep
spyware
oncheckedchanged
.nettiers
xsdobjectgen
nsviewanimation
rescale
swing-app-framework
project-hosting
scripting-languages





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm