我尝试按以下顺序设置员工出勤日期。
row data
emp_no date time
001 2019-08-07 00:00:00 1899-12-30 08:29:28
001 2019-08-07 00:00:00 1899-12-30 17:29:28
我想准备像样并保存在其他表格中
emp_no date time_in time_out
001 2019-08-07 08:29:28 17:29:28
time in 03:00 am - 12:15 pm
time out 12:15 pm to 03:00 am
行数据通过指纹机获取。 每天记录2000名员工详细信息。如何解决此问题?我尝试根据emp_no和date分组,但输出错误 我正在使用php和MySQL进行开发
答案 0 :(得分:1)
这可能有帮助。
create table new_table(emp_no number,
day_date date,
time_in varchar2(15),
time_out varchar2(15));
insert into new_table
select emp_no,
date,
extract(hour from min(time)) || ':' || extract(minute from min(time)) || ':' || extract(second from min(time)),
extract(hour from max(time)) || ':' || extract(minute from max(time)) || ':' || extract(second from max(time))
from employee
group by emp_no, date;
select * from new_table;
尽管我不确定您是从数据中得到什么
time in 03:00 am - 12:15 pm
time out 12:15 pm to 03:00 am
答案 1 :(得分:0)
这是一个主意。获取每个emp_no的行号(按empno,日期和时间排序)。然后将time_out(第二个条目)加入到您的第一个表中。
SELECT t1.emp_no, t1.date, t1.time as time_in, IFNULL(t2.type, 'no time out') as time_out FROM (SELECT
@row_number:=CASE
WHEN @id = emp_no THEN @row_number + 1
ELSE 1
END AS num,
@id:=emp_no as emp_no,
date,
time
FROM
table1,(SELECT @id:=0,@row_number:=0) as t
ORDER BY emp_no, date, time) as t1
LEFT JOIN
(SELECT
@row_number:=CASE
WHEN @id = emp_no THEN @row_number + 1
ELSE 1
END AS num,
@id:=emp_no as emp_no,
date,
time
FROM
table1,(SELECT @id:=0,@row_number:=0) as t
ORDER BY emp_no, date, time) as t2 on t2.num - 1 = t1.num and t2.emp_no = t1.emp_no
WHERE mod(t1.num, 2) = 1