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

Insert value from textbox into 2 different table ms-access
Access 2013 Expression Builder not displaying
Missing ACCDB database drivers
MS Access Form button that allows user to browse/choose file, then imports file to a table
Can codesmith generator work with MS Access provider?
Access file unable to run in x64
Form.Seltop not selecting top most record
I have an ADO recordset, now I want to create a table from it but not at its connection location. How?
weighted average caluculation in access table
inputting info from one text box to another with command button
Sending attachments using access vba
Removing certain strings in an Access 2010 query
MS Access - Hide Columns on Subform
How to fix in oracle sql developer server new connection acces tab missing?
Invalid Syntax using DLookUp
How can I make a form to open by pressing a key combination in Ms Access 2010?

Categories

HOME
wso2
minimum-spanning-tree
push-notification
nullpointerexception
cplex
ckeditor
enterprise-library-5
esper
node-pdfkit
google-cloud-spanner
circular-dependency
hapi
jsprit
worldwind
openrefine
windows-7-x64
angular-ui
scriptcs
intentfilter
mps
sox
google-search-api
wijmo
java-7
fish
poltergeist
brunch
subdomains
create-table
azure-sql-database
file-format
devextreme
dosbox
qwerty
webix-treetable
jna
ansible-playbook
angularjs-factory
unoconv
android-tabhost
ensembles
gulp-sourcemaps
vao
.net-4.6.2
dds
android-cursor
np-complete
sqlbulkcopy
pintos
ableton-live
angular-cache
heidisql
paypal-express
pagedlist
moveit
natvis
rtbkit
xna-4.0
retina
atk4
browser-bugs
createprocessasuser
skos
pick
java.util.concurrent
php-5.4
intentservice
dexterity
apc
easy-install
shellexecute
sqlperformance
ember-app-kit
specification-pattern
chuck
venn-diagram
subscript
eclipse-templates
android-sdk-2.1
imac
xfbml
3gp

Resources

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
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App