1

我有一个数据库,我正在绑定到我们的访问控制系统中。我正在尝试创建一个报告,显示用户从建筑物的第一个入口和最后一个出口。我已经将一个查询放在一起,该查询如何为一个用户提供正确的单个结果,但我想自动执行此操作以遍历用户列表。

获取一个用户的第一个条目的查询(目前是这样)如下:

SELECT TOP 1 AccessLog.ID, AccessLog.Date, First(AccessLog.Time) AS FirstOfTime, AccessLog.User, AccessLog.Details, AccessLog.Event, AccessLog.Department, AccessLog.Where
FROM AccessLog
GROUP BY AccessLog.ID, AccessLog.Date, AccessLog.User, AccessLog.Details, AccessLog.Event, AccessLog.Department, AccessLog.Where
HAVING (((AccessLog.Date)=#DATE#) AND ((AccessLog.User)="USERNAME") AND ((AccessLog.Where)="Front Door (In)"))
ORDER BY First(AccessLog.Time);

我有一个包含所有用户名的单独表格,是否可以为此进行运行?

基本上我是一个完整的 Access n00b,过去我会使用 PHP 和循环函数完成此操作,但这在 Access 中应该很容易实现,不是吗?非常感谢任何帮助。

4

1 回答 1

0

您可以使用以下查询按日期获取所有用户的到达时间列表。将查询定义保存在名称“ArriveTimes”下:

SELECT AccessLog.Date, AccessLog.User, Min(AccessLog.Time) AS ArriveTime
FROM AccessLog
WHERE (((AccessLog.Where)="Front Door (In)"))
GROUP BY AccessLog.Date, AccessLog.User;

同样,您可以使用保存为“DepartTimes”的类似查询来获取出发时间:

SELECT AccessLog.Date, AccessLog.User, Max(AccessLog.Time) AS DepartTime
FROM AccessLog
WHERE (((AccessLog.Where)="Front Door (Out)"))
GROUP BY AccessLog.Date, AccessLog.User;

然后,您可以将 [ArriveTimes] 和 [DepartTimes] 查询连接在一起(在 [Date] 和 [User] 上加入)并通过加入 [AccessLog] 表的两个实例(在 [Date] 上加入,[用户]和[时间]):

SELECT ArriveTimes.Date, ArriveTimes.User, 
    ArriveTimes.ArriveTime, AccessLog_A.Details AS ArriveDetails, 
    DepartTimes.DepartTime, AccessLog_D.Details AS DepartDetails
FROM 
    (AccessLog AS AccessLog_A INNER JOIN 
        (ArriveTimes INNER JOIN DepartTimes 
            ON (ArriveTimes.User = DepartTimes.User) 
                AND (ArriveTimes.Date = DepartTimes.Date)
        ) 
        ON (ArriveTimes.ArriveTime = AccessLog_A.Time) 
            AND (AccessLog_A.User = ArriveTimes.User) 
            AND (AccessLog_A.Date = ArriveTimes.Date)
    ) 
    INNER JOIN AccessLog AS AccessLog_D 
        ON (DepartTimes.DepartTime = AccessLog_D.Time) 
            AND (DepartTimes.User = AccessLog_D.User) 
            AND (DepartTimes.Date = AccessLog_D.Date);
于 2013-03-11T13:17:30.393 回答