我创建了一个表(Attendance)来存储计算,我正在更新表以填充大部分计算。我需要弄清楚员工(EMP_NB)何时离职。这是由TIMEDIFF(ADDTIME(PERIOD, '1:00'), SEC_TO_TIME(Activity_Secs))
计算的。
我的问题是我无法找到一种方法来获取每个日期每个EMP_NB的MAX(PERIOD),同时也抓住相应的Activity_Secs。
TABLEA
╭────────┬──────┬────────┬───────────────╮ │ EMP_NB │ DATE │ Period │ Activity_Secs │ ╰────────┴──────┴────────┴───────────────╯
这是我为了获得员工登录的第一个时期而制作的MySQL代码。
UPDATE calc_Attendance AS att, tableA AS a
JOIN (
SELECT
EMP_NB,
DATE,
Period,
Activity_Secs
FROM tableA
GROUP BY EMP_NB, DATE, Period
) m
ON a.EMP_NB = m.EMP_NB and a.DATE = m.DATE and a.Period = m.Period
SET DEPTARTURE_ACTUAL = TIMEDIFF(ADDTIME(m.Period, '1:00'), SEC_TO_TIME(m.Activity_Secs))
WHERE att.EMP_NB = m.EMP_NB AND att.DATE = m.DATE
;
任何帮助将不胜感激。我没有很多SQL经验,所以这让我疯了。
答案 0 :(得分:0)
UPDATE calc_Attendance AS att, tableA AS a
JOIN (
SELECT
EMP_NB,
DATE,
MAX(Period) AS Period,
Activity_Secs
FROM tableA
GROUP BY EMP_NB,DATE,Activity_Secs
) m
ON a.EMP_NB = m.EMP_NB and a.DATE = m.DATE and a.Period = m.Period
SET DEPTARTURE_ACTUAL = TIMEDIFF(ADDTIME(m.Period, '1:00'), SEC_TO_TIME(m.Activity_Secs))
WHERE att.EMP_NB = m.EMP_NB AND att.DATE = m.DATE
;
答案 1 :(得分:0)
不确定查询中tableA
的用途,但我认为以下内容应该有效。
UPDATE calc_Attendance AS att
JOIN (
SELECT *
FROM (
SELECT EMP_NB, DATE, Period, Activity_Secs
FROM tableA
ORDER BY Period DESC
) m1
GROUP BY EMP_NB, DATE
) m
ON att.EMP_NB = m.EMP_NB and att.DATE = m.DATE
SET DEPTARTURE_ACTUAL = TIMEDIFF(ADDTIME(m.Period, '1:00'), SEC_TO_TIME(m.Activity_Secs));