473,396 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Select record based on max value

I have a database that I am using to record some basic patient information.

The tables I have include:
- PatientDB [main table - primary key is Patient_UID].
- Locations [holds names of different locations in a hospital linked to PatientDB by its primary key Location_UID].

- Analgesia_Primary [holds names of pain relieving methods, linked to PatientDB by its primary key Analgesia_UID] - there are 2 other similar tables that record different classes of pain relief in the same way using primary key link.

- DailyAssessment [this table has a many-to-one relationship with PatientDB]

The DailyAssessment table has its own primary key, Assessment_UID to record individual assessments for each patient, and links to PatientDB table using an integer value which is the same as Patient_UID.

What I am trying to do is get a query which will produce (for a report):

All patient details that are active (using criteria PatientDB.Active=True), the names of the different pain relief options they are on from other tables, and just the most recent entry for that patient from DailyAssessment (I have been using the max value).

I cannot seem to get everything I want from one query and have experimented with sub-queries but do not seem to get anywhere, and often end up with about 60 records being returned even though I do not have that many patients!

Any suggestions would be appreciated, and I can provide more field names etc as needed.

I am using Access 2007.
Sep 26 '18 #1
8 1173
twinnyfo
3,653 Expert Mod 2GB
gwarnock85,

Welcome to Bytes!

I do not see you accomplishing this with just one query. Primarily, if you are looking for the most recent Assessment, that will have to be a subquery to get the max. Granted, that "can" be embedded in the SQL string itself, but it is a subquery nonetheless.

If you would be so pleasant as to post what you have tried so far, that would be great. Then we can look at it and offer suggestions.

Hope we can hepp!
Sep 26 '18 #2
PhilOfWalton
1,430 Expert 1GB
Try building a query based on the Patients, location and something like:-

Expand|Select|Wrap|Line Numbers
  1. DLookup("Assesment", "DailyAssessment", "PatientID = " & PatientID & " AND AssementDate = " & DMax("AssesmentDate", "DailyAssessment", "PatientID = " & PatientID))
Sorry, that is air code so there may well be errors.

Base your Report on the above, and have 3 sub reports to show the various pain reliefs used.

Phil
Sep 26 '18 #3
twinnyfo
3,653 Expert Mod 2GB
Good idea, Phil! I forgot about using the DMax() function. That will certainly work well if all the syntax gets worked out.
Sep 26 '18 #4
NeoPa
32,556 Expert Mod 16PB
While subqueries can make a query a fair bit more complicated, especially for those who are new to it, using Domain Aggregate queries (such as DLookup()) is something to be warned against. It's an approach that makes it much harder for the SQL engine (Jet or ACE when using native Access but others are available via ODBC of course.) to determine the most efficient approach for you.

It can help to get the results you need but if you need to expand on it later then it's an inbuilt flaw.

It can be great for use within code, but just be aware that it's not generally recommended for use within SQL or queries.
Sep 26 '18 #5
PhilOfWalton
1,430 Expert 1GB
@Neopa

I agree, Domain Aggregate functions should be avoided, and given the actual table structure, it may me possible to use Sub Queries. As you say, they are not easy, and I am vert far from being proficient at designing them.

My rather primitive method would be to build a Totals Query to find the Maximum Date for that Patient's assessment, then link that query to another query based on the patient and DailyAssment and join the 2 PatientIDs and the Assessment Date.

Possibly if gwarnock85 were to post an image of his relationship window, he might get more concrete advice.

Phil
Sep 26 '18 #6
NeoPa
32,556 Expert Mod 16PB
It sounds like your primitive method is pretty well bang-on then Phil ;-)

There's always an option to contain the one query within the other using Subqueries in SQL, but that isn't the only way. A defined QueryDef object works just as well. Particularly as far as the SQL engine is concerned. It's very impressive what even Jet & ACE can do as far as optimising queries goes.
Sep 26 '18 #7
twinnyfo
3,653 Expert Mod 2GB
Side question (but related), NeoPa,

Whether the query is built as a separate QueryDef or whether it is embedded within the SQL itself, aren't both methods "essentially" using a SubQuery? One version uses a predefined Object, namely a QueryDef, the other uses the same SQL used for the QueryDef but inserts it into the main Query--both are then JOINed to one of the main query's keys or used as criteria.

Just want to make sure I understand the nature of SubQueries.
Sep 27 '18 #8
NeoPa
32,556 Expert Mod 16PB
TwinnyFo:
Whether the query is built as a separate QueryDef or whether it is embedded within the SQL itself, aren't both methods "essentially" using a SubQuery?
Yes. You have that perfectly correct.
Sep 27 '18 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve | last post by:
Does any anyone have a procedure for a query where a calculated field returns the previous record's value in another field. For example: A F A Z F M Z The primary key is in random...
1
by: arthur-e | last post by:
How can you select records based on more than one combo box - I have a combobox that selects records based on name (I'm sure this has been asked a thousand times - web site answer/link could be...
1
by: sangram | last post by:
how to select a default value of combo box in run time, i have select some value from database ,depending on this i have to set the value of combox box... advance thanks..
13
by: helm | last post by:
Folks, There's some great help, in this forum on the combo box Not in List event and it works fine for me. I can recognise the event, accept the newdata, add some additional record info via a form...
1
by: =?Utf-8?B?QmFkaXM=?= | last post by:
Hi, In my table I have a column called ID that contain values such as A1,A2,AA1,AG3...AU and I would like to select the maximum value in the values that start with A followed by number in this...
2
by: Shivajirp | last post by:
I have table in which Shift no, Start time. End time. if start time of shift is 6pm and end time of shift is 6 am then I want to select record between start time and end time.I need query to selct...
5
by: agarwasa2008 | last post by:
Hi, I would like to delete a record based on a user entered string. Here are the details. I have a txtFind textbox. A string is entered by the user. Based on that string value it displays that...
6
by: mukeshrasm | last post by:
Hi I want to update records in database based on selected records using checkbox. if user does not select a record and clicks update button it should show the message please select record to be...
11
by: prashantdixit | last post by:
Hi, I am developing a stock control software. Iam stuck somewhere. I have a form "Add new stock" consisting of combobox, text boxes etc. which is used to add records in a table. I have another...
25
TheSmileyCoder
by: TheSmileyCoder | last post by:
I have a table of names, with 2 fields in it, dblProbability and txName, where dblProbabilty is the relative probability that the Name in that row should be selected. How can I get 1 random name from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.