在Access中将多个查询组合在一个中

时间:2013-07-03 17:02:17

标签: ms-access ms-access-2003

我想在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;

一个显示每个日期的总记录,另一个显示标记为缺席的记录。

1 个答案:

答案 0 :(得分:0)

将您的2个查询用作子查询。 LEFT JOIN注册子查询到缺勤子查询...将返回所有AttendDate值的行,无论这些日期是否记录了缺席。

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;

如果您要在Access会话中运行此查询,则可以使用Nz(a.Absent, 0)将空Absent值显示为零。如果从Access外部运行查询,请使用IIf(a.Absent Is Null, 0, a.Absent)