我有2个表要加入;我希望雇员表中的所有行都可以检索交易表中日期的最新时间;事务表可以在同一日期为员工提供多个记录。目前,无法找到将交易限制在最近记录(最新输入)的方法
这是一个访问表;从Excel进行Adodb查询以访问。
SELECT E.ID, E.Employee, E.lastname, T.in, T.out, T.date, T.type, T.EmployeeID FROM employee E RIGHT JOIN transactions T ON E.ID = INT(T.EmployeeID) WHERE " & _
"T.date = #" & Format(yesterdate, "Short Date") & "# AND T.type = 'JobCode5' AND E.location = 'EastBuilding' AND E.ID = 89 AND E.stat = TRUE
我希望每位适用的员工获得1笔交易;最好是在给定日期的最近时间进行交易。
答案 0 :(得分:0)
您可以使用相关子查询来获取最新交易。 MS Access中有一个技巧可以处理没有事务的员工:
SELECT E.ID, E.Employee, E.lastname,
T.in, T.out, T.date, T.type, T.EmployeeID
FROM employee as E LEFT JOIN
transactions as T
ON E.ID = INT(T.EmployeeID)
WHERE (T.out = (SELECT MAX(t2.out)
FROM transactions as T2
WHERE T2.EmployeeID = T.EmployeeID AND
T2.Date = T.Date AND
T.type = 'JobCode5'
) OR
T.date IS NULL
) AND
<your filters here>
答案 1 :(得分:0)
子查询可以完成工作,但运行速度很慢
SELECT E.employee, E.lastname, (SELECT TOP 1 IIF(ISNULL([in]), '', [in]) FROM [transactions] WHERE [date] = #6/3/2019# AND [type] = 'JobCode5' AND INT([EmployeeID]) = E.ID ORDER BY transactions.ID DESC) AS IN_TIME, " & _
"(SELECT TOP 1 IIF(ISNULL([out]), '', [out]) FROM [transactions] WHERE [date] = #6/3/2019# AND [type] = 'JobCode5' AND INT([EmployeeID]) = E.ID ORDER BY transactions.ID DESC) AS AS_TIME FROM employee E WHERE E.stat = True AND E.location = 'EastBuilding'