Some details: Query1 returns rows that are duplicates except for the DispositionID value. I want only the rows with the highest DispositionID value.
Simplified example:
Query1 returns:
DispositionID [Last Name] [First Name]
0 Smith Bob
1422 Smith Bob
0 Alexander Grant
0 Nalbur Betty
What I want:
DispositionID [Last Name] [First Name]
1422 Smith Bob
0 Alexander Grant
0 Nalbur Betty
Thanks for any guidance. I'm terrible with getting aggregate functions to work and it seems like using an alias of Q1 for the Query1 inside of Query2 should solve the problem but I cannot get that to work either.
What I've tried and the detailed actual queries:
I have been struggling to figure out a way to combine the following 2 queries into a single query, but when I use aliases, I get errors with the SQL. Is it possible to combine these 2 queries into a single query, or will I have to use 2 separate queries?
Query1:
SELECT tblCLient_Dispositions.DispositionID, tblClients.[Last Name], tblClients.[First Name], tblClients.[Date of Contact], tblCLient_Dispositions.StartDate, tblCLient_Dispositions.DispositionStatusID, tblStudentStatus.StudentStatus, tblStudentStatus.StudentProfessor, tblStudentStatus.ProfessorEmail, (SELECT COUNT(*)
FROM
(
SELECT tblClient_Disp_Appointments.DispositionID,
tblClient_Disp_Appointments.ApptTypeID,
tblClient_Disp_Appointments.ApptStatusID,
tblClient_Disp_Appointments.ApptDate,
tblroomSchedule.RoomID
FROM (tblClient_Disp_Appointments
LEFT JOIN tblroomSchedule ON tblClient_Disp_Appointments.AppointmentID = tblroomSchedule.ApptID)
WHERE (RoomID <> 6)
AND (ApptTypeID = 23 OR ApptTypeID = 18 OR ApptTypeID = 3 OR ApptTypeID = 11 OR ApptTypeID = 6 OR ApptTypeID = 19 OR ApptTypeID = 2)
AND (ApptStatusID = 3)
)
WHERE (DispositionID = tblCLient_Dispositions.DispositionID)
) AS SessionsCompleted, tblStudentStatus.SessionsRequired, CDate(forms!frmProfessorReport.tbxStartingDate) AS WhatYear
FROM (tblCLient_Dispositions LEFT JOIN tblClients ON tblCLient_Dispositions.ClientID = tblClients.ClientID) LEFT JOIN tblStudentStatus ON tblClients.University_Student = tblStudentStatus.StudentStatusID
WHERE (((tblClients.University_Student)=[forms]![frmProfessorReport].[cbxWhichClass]) AND ((Nz(Year([tblCLients].[Date of Contact]),Year([forms]![frmProfessorReport].[tbxStartingDate])))=Year([forms]![frmProfessorReport].[tbxStartingDate])))
ORDER BY tblClients.[Last Name]
UNION
SELECT 0 AS DispositionID, tblClients.[Last Name], tblClients.[First Name], tblClients.[Date of Contact], Null AS StartDate, 0 AS DispositionStatusID, tblStudentStatus.StudentStatus, tblStudentStatus.StudentProfessor, tblStudentStatus.ProfessorEmail, 0 AS SessionsCompleted, tblStudentStatus.SessionsRequired, CDate(forms!frmProfessorReport.tbxStartingDate) AS WhatYear
FROM tblClients LEFT JOIN tblStudentStatus ON tblClients.University_Student = tblStudentStatus.StudentStatusID
WHERE (((tblClients.University_Student)=[forms]![frmProfessorReport].[cbxWhichClass])
AND ((Nz(Year([tblCLients].[Date of Contact]),Year([forms]![frmProfessorReport].[tbxStartingDate])))=Year([forms]![frmProfessorReport].[tbxStartingDate])))
ORDER BY tblClients.[Last Name]
Query2:
SELECT *
FROM Query1
WHERE DispositionID = (SELECT MAX(DispositionID) FROM Query1 AS lookup WHERE lookup.[Last Name] & lookup.[First Name] & lookup.[Date of Contact] = Query1.[Last Name] & Query1.[First Name] & Query1.[Date of Contact])
What I want to do is paste in Query1 into "Query1" where Query2 says "FROM Query1" and then give it an alias of Q1, but then I get an error in the WHERE clause of Query2 that Access cannot find Q1. (Of course, I change the references in the WHERE clause from Query1 to Q1 before running the combined query SQL.)
Based on your simplified example, you would seem to want:
select max(DispositionID), [Last Name], [First Name]
from t
group by [Last Name], [First Name]