Help with Selecting Duplicate Records

Fowz

Registered User.
Local time
Today, 11:19
Joined
Jan 25, 2014
Messages
14
Hi all,

I'll first of all explain the purpose of the query. I've built a database to record the admission details of patients admitted to hospital. Amongst other things, the database captures date/time of admission and date/time of discharge.

The query I am building needs to show patients with multiple admissions, and in particular, the number of days that have elapsed since they were last discharged. Within my query is the following subquery that I had written to show this aspect of it:

(SELECT TOP 1 Dupe.[Discharge Date]
FROM [Inpatient Database] AS Dupe
WHERE Dupe.[CHI Number] = [Inpatient Database].[CHI Number]
AND Dupe.[Discharge Date] <= [Inpatient Database].[Admission Date]) AS [Previous Discharge],

Note: "CHI Number" is a unique reference number assigned to every patient.

I then display in a column [Admission Date] - [Previous Discharge].

This works fine where a patient only has one previous admission. However, where a patient has multiple admission, the subquery always returns that patient's first discharge date instead of their most recent discharge date (because I have used the "TOP 1" predicate). It seems that it should be straightforward enough, however I can't seem to work around it. Anything I try results in errors, and so I keep defaulting back to my "TOP 1" solution.

ie. What I want it to show is:
Name.......Admission Date...Discharge Date.... Previous Discharge... Days Between Admissions
Joe Bloggs ..01/01/2014 .......10/01/2014
Joe Bloggs ..15/01/2014 .......20/01/2014 .......10/01/2014 ..............5
Joe Bloggs ..27/01/2014 .......01/02/2014 ........20/01/2014 .............7

However, what it actually shows is:
Name ......Admission Date ...Discharge Date ...Previous Discharge ...Days Between Admissions
Joe Bloggs ..01/01/2014 ......10/01/2014
Joe Bloggs ..15/01/2014 ......20/01/2014 ........10/01/2014 .............5
Joe Bloggs ..27/01/2014 ......01/02/2014 ........10/01/2014 .............17


Any help?
 
I don't think you need a sub-query. Just a DMax (http://www.techonthenet.com/access/functions/domain/dmax.php) to pull the latest discharge date that isn't greater than the one you are currently looking at:

If you can provide the structure of the table I could help you more specifically (include field names and table name)
 
Fantastic! Thanks for your help, that has solved it.

I used "MAX" as a predicate for "SELECT", (ie. Select Max(Dupe.[Discharge Date])) and it has worked perfectly.

It would have been helpful for Access Help to have provided an extensive list of predicates rather than just All, Top, Distinct, DistinctRow, giving the impression that those were the only options.

Many thanks for your swift reply, also. Much appreciated.
 

Users who are viewing this thread

Back
Top Bottom