I created a table (Attendance) to store calculations, and I am updating the table to fill in the majority of the computations. I need to figure out when an employee (EMP_NB) left work for the day. This is calculated by TIMEDIFF(ADDTIME(PERIOD, '1:00'), SEC_TO_TIME(Activity_Secs)).
My issue is that I can’t figure out a way to grab the MAX(PERIOD) per EMP_NB per DATE, while also grabbing the corresponding Activity_Secs.
tableA
╭────────┬──────┬────────┬───────────────╮ │ EMP_NB │ DATE │ Period │ Activity_Secs │ ╰────────┴──────┴────────┴───────────────╯
Here is the MySQL code I made to get the first period an employee logged in.
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
;
Any help will be appreciated. I don’t have a lot of experience with SQL so this is driving me insane.
1 Answer