我有一个名为“attLog”的表格,里面有empID和他们下班的时间,还有serialNo,它按1计数。我在内部使用一个称为“employees”的表来连接这个表,但这样做时,它会给我最接近顶部的结果,但我需要最新或最底部的结果。
dbo.attLog
empID | time | DATE | serialNo
------+--------+-----------+----------
1001 | 5.01PM | 2020-10-1 | 1
1002 | 5.04PM | 2020-10-1 | 2
1002 | 5.02PM | 2020-10-2 | 3
1001 | 5.04PM | 2020-10-2 | 4
1002 | 5.15PM | 2020-10-2 | 5这是我运行查询时得到的结果。
dbo.employees
empID | latestTime
------+-----------
1001 | 5.04PM
1002 | 5.02PM这就是我希望发生的结果。
dbo.employees
empID | latestTime
------+-----------
1001 | 5.04PM
1002 | 5.15PM下面是我正在使用的查询。
UPDATE employees
SET employees.latestTime = attLog.time
FROM employees employees
INNER JOIN attLog attLog ON employees.empID = attLog.empID
WHERE attLog.authDate = '2020-10-2'非常感谢。任何帮助都将不胜感激。
发布于 2020-11-02 10:44:30
先聚合表,然后连接:
UPDATE e
SET e.latestTime = al.max_time
FROM employees e JOIN
(SELECT al.empId, MAX(al.time) as max_time
FROM attLog al
WHERE al.authDate = '2020-10-02'
GROUP BY al.empId
) al
ON e.empID = al.empID;https://stackoverflow.com/questions/64639168
复制相似问题