大厂真题SQL 10 题解 | 统计各岗位员工平均工作时长
# 需求分析 #注:如员工未打卡该字段数据会存储为NULL,那么不计入在内。 # 保留三位小数 # 平均时间排序,where ,排序,group by # 分钟计算为小时,打卡 TIMESTAMPDIFF,MINUTE不能加引号 # timstampdiff 先小后大,和datediff不一样 SELECT a.post,ROUND(AVG(TIMESTAMPDIFF(minute,first_clockin,last_clockin)/60),3) as work_hours FROM staff_tb a left join attendent_tb b on a.staff_id=b.staff_id where TIMESTAMPDIFF(minute,first_clockin,last_clockin)>0 group by a.post order by ROUND(AVG(TIMESTAMPDIFF(minute,first_clockin,last_clockin)/60),3) desc