Question

I have two queries that I want to combine into one in Microsoft Access 2003.

SELECT AttendanceQuery.AttendDate, Count(*) AS Absent
FROM AttendanceQuery
WHERE (((AttendanceQuery.Present)=False))
GROUP BY AttendanceQuery.AttendDate;

SELECT AttendanceQuery.AttendDate, Count(*) AS Enrollment
FROM AttendanceQuery
GROUP BY AttendanceQuery.AttendDate;

The one shows the total records for each date, the other shows the ones that are marked absent.

Was it helpful?

Solution

Use your 2 queries as subqueries. LEFT JOIN the enrollment subquery to the absences subquery ... that will return rows for all AttendDate values whether or not those dates had absences recorded.

SELECT e.AttendDate, e.Enrollment, a.Absent
FROM
    (
        SELECT a1.AttendDate, Count(*) AS Enrollment
        FROM AttendanceQuery AS a1
        GROUP BY a1.AttendDate
    ) AS e
    LEFT JOIN
    (
        SELECT a2.AttendDate, Count(*) AS Absent
        FROM AttendanceQuery AS a2
        WHERE a2.Present=False
        GROUP BY a2.AttendDate
    ) AS a
    ON e.AttendDate = a.attendDate;

If you will be running this query from within an Access session, you can use Nz(a.Absent, 0) to display Null Absent values as zero. If you run the query from outside Access, use IIf(a.Absent Is Null, 0, a.Absent)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top