目前,我一直在寻找最佳查询来制作出勤记录数据库的简历。
这是“ absen”表的当前布局:
id|employee_id| log |status
1 | 1 | 2019-03-27 07:58:32 | in
2 | 1 | 2019-03-27 07:58:38 | in
3 | 1 | 2019-03-27 17:12:34 | out
4 | 2 | 2019-03-26 07:45:12 | in
5 | 2 | 2019-03-26 18:22:54 | out
6 | 1 | 2019-03-28 07:45:38 | in
7 | 1 | 2019-03-28 17:10:55 | out
8 | 2 | 2019-03-27 07:59:02 | in
9 | 2 | 2019-03-27 18:35:16 | out
这是目标
employee_id| in | out
2 | 2019-03-26 07:45:12 | 2019-03-26 18:22:54
1 | 2019-03-27 07:58:32 | 2019-03-27 17:12:34
2 | 2019-03-27 07:59:02 | 2019-03-27 18:35:16
1 | 2019-03-28 07:45:38 | 2019-03-28 17:10:55
(在ASC中为ORDER BY)
我已经尝试过以下查询:
SELECT
dateng.e_idIN AS "PIN",
MASUK,
PULANG
FROM
(
SELECT
MIN(log) AS "MASUK",
employee_id AS e_idIN
FROM
absen
WHERE
status = "in"
GROUP BY
log
) dateng
INNER JOIN(
SELECT
MAX(log) AS "PULANG",
employee_id AS e_idOUT
FROM
absen
WHERE
status = "out"
GROUP BY
log
) pergi
ON
dateng.e_idIN = pergi.e_idOUT
但结果与预期不符
PIN | MASUK | PULANG
2 | 2019-03-26 07:45:12 | 2019-03-27 18:35:16
2 | 2019-03-26 07:45:12 | 2019-03-26 18:22:54
1 | 2019-03-27 07:58:32 | 2019-03-28 17:10:55
1 | 2019-03-27 07:58:32 | 2019-03-27 17:12:34
1 | 2019-03-27 07:58:38 | 2019-03-28 17:10:55
1 | 2019-03-27 07:58:38 | 2019-03-27 17:12:34
2 | 2019-03-27 07:59:02 | 2019-03-27 18:35:16
2 | 2019-03-27 07:59:02 | 2019-03-26 18:22:54
1 | 2019-03-28 07:45:38 | 2019-03-28 17:10:55
1 | 2019-03-28 07:45:38 | 2019-03-27 17:12:34
此任务有什么线索吗?
答案 0 :(得分:0)
我认为这会起作用:
select t.employee_id, min(t.login) login, t.logout
from (
select a.employee_id, a.log login,
(select min(log) from absen
where status = 'out' and employee_id = a.employee_id and log > a.log) logout
from absen a
where a.status = 'in'
) t
group by t.employee_id, t.logout
order by t.login, t.logout