[SQL] Find out the employee’s daily clock-in and clock-in time from an attendance table tb_cwa---Part 2

Thinking of following the previous article https://blog.csdn.net/debimeng/article/details/104113932

If the clock-in time stored by the company is not only two clock-ins when you go to and from work, but also includes clock-ins when you go out to eat at noon, that means every person has more than 2 clock-ins a day; in
this case, the SQL written in the previous article will not be accurate. Commuting time,

The relevant fields of the attendance sheet are as follows: ID, NAME, NO, TIME

ID  NAME         NO          TIME
1   zhangsan    1001         2020-01-01 08:34:40
2   lisi        1002         2020-01-01 08:39:29
3   wangwu      1003         2020-01-01 08:44:38
4   zhangsan    1001         2020-01-01 18:24:18
5   lisi        1002         2020-01-01 18:34:28
6   wangwu      1003         2020-01-01 18:54:20
7   wangwu      1003         2020-01-01 12:34:20
8   wangwu      1003         2020-01-01 12:54:20
日期          姓名              上班时间                    下班时间
2020-01-01    zhangsan          2020-01-01 08:34:40         2020-01-01 18:24:18
2020-01-01    lisi              2020-01-01 08:39:29         2020-01-01 18:34:28
2020-01-01    wangwu            2020-01-01 08:44:38         2020-01-01 18:54:20

solve:

select date_format(time,'%Y-%m-%d')     as '日期'
      ,name                             as '姓名'
      ,min(time)                        as '上班时间'
      ,max(time)                        as '下班时间'
  from tb_cwa
 group by name,date_format(time,'%Y-%m-%d')
;

日期          姓名          上班时间                    下班时间
2020-01-01  zhangsan        2020-01-01 08:34:40         2020-01-01 18:24:18
2020-01-01  lisi            2020-01-01 08:39:29         2020-01-01 18:34:28
2020-01-01  wangwu          2020-01-01 08:44:38         2020-01-01 18:54:20

Remarks: table creation and data

create table tb_cwa(id int,name varchar(10),no int,time datetime);
insert into tb_cwa values(1,'zhangsan',1001,'2020-01-01 08:34:40');
insert into tb_cwa values(2,'lisi',1002,'2020-01-01 08:39:29');
insert into tb_cwa values(3,'wangwu',1003,'2020-01-01 08:44:38');
insert into tb_cwa values(4,'zhangsan',1001,'2020-01-01 18:24:18');
insert into tb_cwa values(5,'lisi',1002,'2020-01-01 18:34:28');
insert into tb_cwa values(6,'wangwu',1003,'2020-01-01 18:54:20');
insert into tb_cwa values(7,'wangwu',1003,'2020-01-01 12:34:20');
insert into tb_cwa values(8,'wangwu',1003,'2020-01-01 12:54:20');

 

Guess you like

Origin blog.csdn.net/debimeng/article/details/104117288