SQL - GroupBy and Max to return all fields?

I'm trying to get some kind of query to work for a report.  Here are the details.  Please bear with me as this is a bit of a long explaination though simple in spirit :)

There are two tables linked by a keyfield.  The relationship is one to many.  So table A links to many in table B by that key field.  Keep in mind that this key field is not the primary key in table B but it is indexed(FYI).  Anyway, there is a date field in table B for which I am trying to get the latest of in a group of common  values made from the key link field.  

The catch seems to be that in order to utilize the GroupBy() and the Max() functions you need to follow certain rules.  It seem that these rules do not allow the use of wildcards so as to include all the fields in a query result.  Thus, I resolved to use three queries to produce the results that I need.  1.) a query that gets the latest date and one other field to link to from table B. 2.) an inner join to get all fields in table B linked to each row returned from the first query. 3.) a query linking table A to table B to get all fields from both tables where each row then has the latest date in B.

The end results preferrably would be that all fields from both tables are in a single returned row.  Each row contains the latest date from the group of records with a common linked key field.

While this appears to work in Access through SQL views and trial and error, I have a somewhat more difficult time getting it to work in an MFC application with an embedded SQL call through DAO since I'm using a single recordset to contain the results.  The main question is can this be done with a single query in Access SQL?  And if not, would a querydef call to the main query above be the most efficient way to call this query in my application?

Here are my current queries:

from Table B (with latest dates)
================================
SELECT Exam.PatientAutoKey, Max(Exam.Date) AS MaxOfDate
FROM Exam
GROUP BY Exam.PatientAutoKey;

from Table B (gets the rest of the fields linked up with latest date)
==================================
SELECT DISTINCT Exam.PatientAutoKey, Exam.ExamRecordID,Exam.Date,
...all the rest of the fields...
FROM Exam INNER JOIN MaxExamDates ON Exam.PatientAutoKey = MaxExamDates.PatientAutoKey
WHERE (((Exam.Date)=[MaxExamDates].[MaxOfDate]));

from Table A - link all the fields in table A to those in table B.
=====================================
SELECT *
FROM Patient INNER JOIN ExamByMaxDate ON Patient.PatientAutoKey = ExamByMaxDate.PatientAutoKey
ORDER BY Patient.PatientLastName;
ndewaganAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KangaRooCommented:
Why not

SELECT
   Patient.*,
   Exam.*,
   (SELECT MAX(bb.date) FROM Exam as bb WHERE bb.PatientAutoKey = Exam.PatientAutoKey) AS MaxDate
FROM Exam INNER JOIN Patient ON Patient.PatientAutoKey = ExamByMaxDate.PatientAutoKey
ORDER BY Patient.PatientLastName
0
KangaRooCommented:
Oeps:
FROM Exam INNER JOIN Patient ON Patient.PatientAutoKey = Exam.PatientAutoKey
0
ndewaganAuthor Commented:
I don't understand the your use of bb fully since it seems to be both a TakeThePlaceOfName and a table source in the same sub-query section.  Should it be something like MaxExamDate?

I've gone ahead and tried the following from your query model:
SELECT
   Patient.*,
   Exam.*,
   (SELECT MAX(Exam.date) FROM Exam WHERE Patient.PatientAutoKey = Exam.PatientAutoKey) AS MaxDate
FROM Exam INNER JOIN Patient ON Patient.PatientAutoKey = Exam.PatientAutoKey
ORDER BY Patient.PatientLastName

....but got a totaling of all records.  I even tried your exact query with bb in it and with the ExamByMaxDate replaced with just Exam and still got a totalling of all records instead of just the latest dated record of the joins.  I suspect it is because there is no grouping on any particular field such as the Exam.Date...?

Any other suggestions?
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

ndewaganAuthor Commented:
I should also mention one other little complication:
I'm trying to avoid quering for two very large fields with in the Exam table so the use of Exam.* may not work so good for me.  Hence, I have been putting in the list of specific fields in my Exam part of the query.
0
KangaRooCommented:
Then you only put the desired fields in the SELECT piece of the query.

bb is an alias for the table Exam in the sub-query.

I see now, you only want one record for each patient, the max date:

SELECT
   Patient.name
   , Exam.Date
FROM
  Exam INNER JOIN Patient ON Exam.PatientAutoKey = Patien.PatientAutoKey
WHERE
  Exam.Date >= ALL( SELECT Date FROM Exam
                 WHERE bb.PatientAutoKey = Patient.PatientAutoKey);
0
KangaRooCommented:
Full of typo's...

SELECT
   Patient.name
   , Exam.Date
FROM
  Exam INNER JOIN Patient ON Exam.PatientAutoKey = Patient.PatientAutoKey
WHERE
  Exam.Date >= ALL( SELECT Date FROM Exam
                 WHERE Exam.PatientAutoKey = Patient.PatientAutoKey);
0
ndewaganAuthor Commented:
One other key point which might not have been given enough weight:
As far as the Exam data (which gets joined with the Patient data at some point) I wish to get the latest dated Exam rows in a grouping of the Exam.PatientAutoKey column.  Ie...

Exam data:
001,111,x,x,x,09/01/1999<--
002,111,x,x,x,09/09/1999
003,111,x,x,x,09/02/1999
004,222,x,x,x,09/03/1999<--
005,222,x,x,x,09/10/1999
006,333,x,x,x,09/01/1999<--
007,333,x,x,x,09/03/1999

where 001,002,003... are Exam primary key values, 111, 222, 333... are PatientAutoKey values, the row I want would be the very first row, the 4th row and the 6th row since they each have the latest dates for a grouping on PatientAutoKey values.

This is all assuming that grouping is needed of course...
0
KangaRooCommented:
Summarizing, you want:
For each patient, give all the exams that were taken on the last date that one or more exams for that patient where taken.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ndewaganAuthor Commented:
One other key point which might not have been given enough weight:
As far as the Exam data (which gets joined with the Patient data at some point) I wish to get the latest dated Exam rows in a grouping of the Exam.PatientAutoKey column.  Ie...

Exam data:
001,111,x,x,x,09/01/1999<--
002,111,x,x,x,09/09/1999
003,111,x,x,x,09/02/1999
004,222,x,x,x,09/03/1999<--
005,222,x,x,x,09/10/1999
006,333,x,x,x,09/01/1999<--
007,333,x,x,x,09/03/1999

where 001,002,003... are Exam primary key values, 111, 222, 333... are PatientAutoKey values, the row I want would be the very first row, the 4th row and the 6th row since they each have the latest dates for a grouping on PatientAutoKey values.

This is all assuming that grouping is needed of course...
0
ndewaganAuthor Commented:
One other key point which might not have been given enough weight:
As far as the Exam data (which gets joined with the Patient data at some point) I wish to get the latest dated Exam rows in a grouping of the Exam.PatientAutoKey column.  Ie...

Exam data:
001,111,x,x,x,09/01/1999<--
002,111,x,x,x,09/09/1999
003,111,x,x,x,09/02/1999
004,222,x,x,x,09/03/1999<--
005,222,x,x,x,09/10/1999
006,333,x,x,x,09/01/1999<--
007,333,x,x,x,09/03/1999

where 001,002,003... are Exam primary key values, 111, 222, 333... are PatientAutoKey values, the row I want would be the very first row, the 4th row and the 6th row since they each have the latest dates for a grouping on PatientAutoKey values.

This is all assuming that grouping is needed of course...
0
ndewaganAuthor Commented:
Ooops, I think I put in bad dates... correction:
Exam data:
001,111,x,x,x,09/09/1999<--
002,111,x,x,x,09/01/1999
003,111,x,x,x,09/03/1999
004,222,x,x,x,09/12/1999<--
005,222,x,x,x,09/01/1999
006,333,x,x,x,09/03/1999<--
007,333,x,x,x,09/01/1999
0
ndewaganAuthor Commented:
Ooops, I think I put in bad dates... correction:
Exam data:
001,111,x,x,x,09/09/1999<--
002,111,x,x,x,09/01/1999
003,111,x,x,x,09/03/1999
004,222,x,x,x,09/12/1999<--
005,222,x,x,x,09/01/1999
006,333,x,x,x,09/03/1999<--
007,333,x,x,x,09/01/1999
0
ndewaganAuthor Commented:
Ooops, I think I put in bad dates... correction:
Exam data:
001,111,x,x,x,09/09/1999<--
002,111,x,x,x,09/01/1999
003,111,x,x,x,09/03/1999
004,222,x,x,x,09/12/1999<--
005,222,x,x,x,09/01/1999
006,333,x,x,x,09/03/1999<--
007,333,x,x,x,09/01/1999
0
ndewaganAuthor Commented:
OK!,  I think your last query worked.  I got the number of records I was looking for.  I don't quite understand fully but I'll give you the points and study it a on my own.  Thanks!
0
KangaRooCommented:
If you have any questions about it you can ask me here. ALL is quite usefull.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Hardware

From novice to tech pro — start learning today.