sql查询,将同一用户多条打卡记录合并成一条输出,哪位大佬指教一下,非常感谢!

图片说明
如果上班或者下班没有打卡,则对应的时间位置备注都为空

https://wenwen.sogou.com/z/q727395295.htm

过程中需要判断的有很多,最好用存储过程或程序。
需要判断的有:
1、打卡时间是上班时间还是下班时间
2、迟到或早退的时间是不是在规定范围内,迟到或早退的时间是多少
3、和请假记录关联
4、如果是弹性工作工时的计算

您好,望参考,如下:
1.表 “tmp_20200617”是临时创建测试表,并插入了部分测试数据 ,如图:
图片说明
2.查询结果sql如下:
select
t1.id,t1.min_dt 上班打卡时间,t1.max_dt 下班打卡时间,
t2.locals 上班地理位置,t3.locals 下班地理位置,
t2.descs 上班打卡备注,t3.descs 下班打卡备注
from (
select id ,min(case when type='上班打卡' then min_dt end) min_dt ,max(case when type='下班打卡' then max_dt end) max_dt
from
(select id,type,min(time) min_dt ,max(time) max_dt from tmp_20200617 group by id,type
)tmp
group by id
)t1
left join (select id,time,locals,descs from tmp_20200617 where type='上班打卡' ) t2
on t1.id=t2.id

and t1.min_dt=t2.time
left join (select id,time,locals,descs from tmp_20200617 where type='下班打卡' ) t3
on t1.id=t3.id

and t1.max_dt=t3.time
;
结果如图:
图片说明

用二叉表算法可以处理的了这样信息。