Hello everyone,
I would need your assistance, please. I am working on a select query from an employee attendance table (I work in an MS ACCESS 2016 environment), but the problem is that this table contains duplicates with slight variations in the timings.
An example might make things clearer.
The source table contains the following data:
EMPLOYEE_ID | LoginTime | LogoutTime
8858043_IPSATMO | 2024-01-06 07:58:48.000 | 2024-01-06 14:03:05.000
8858043_IPSATMO | 2024-01-06 07:59:47.000 | 2024-01-06 14:05:05.000
8858043_IPSATMO | 2024-01-06 15:00:06.000 | 2024-01-06 18:05:40.000
8858043_IPSATMO | 2024-01-06 15:00:29.000 | 2024-01-06 18:01:40.000
Desired results:
EMPLOYEE_ID | LoginTime | LogoutTime
8858043_IPSATMO | 2024-01-06 07:58:48.000 | 2024-01-06 14:05:05.000
8858043_IPSATMO | 2024-01-06 15:00:06.000 | 2024-01-06 18:05:40.000
Using ChatGPT, I obtained the following query that almost works, except it gives me the second connections for each login/logout group and not the first.
Code:
sql
Copy code
SELECT
CD1.EMPLOYEE_ID,
MIN(CD1.LoginTime) AS FirstLogin,
MAX(CD2.LogoutTime) AS LastLogout
FROM
Telephonie AS CD1
LEFT JOIN
Telephonie AS CD2
ON
(CD1.LoginTime < CD2.LogoutTime) AND (CD1.EMPLOYEE_ID = CD2.EMPLOYEE_ID)
WHERE
NOT EXISTS (
SELECT 1
FROM Telephonie AS CD3
WHERE CD3.EMPLOYEE_ID = CD1.EMPLOYEE_ID
AND CD3.LoginTime > CD1.LoginTime
AND CD3.LoginTime < CD2.LogoutTime
)
GROUP BY
CD1.EMPLOYEE_ID, CD1.LoginTime
Can you please help me?
Thank you in advance.