我有一个数据库,我正在进入我们的访问控制系统。我正在尝试创建一个报告,显示用户的第一个入口和最后一个出口。我已经将一个查询放在一起,这给了我一个用户的正确单一结果,但我希望自动执行此操作以运行用户列表。
抓取一个用户(当前)的第一个条目的查询如下:
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中很容易实现,不应该吗?任何帮助非常感谢。
答案 0 :(得分: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);