with shichang as( select staff_id,first_clockin,last_clockin, TIMESTAMPDIFF(MINUTE, first_clockin, last_clockin)/60 as gongzuo from attendent_tb) select post, round(avg(gongzuo),3)as work_hours from shichang join staff_tb on shichang.staff_id=staff_tb.staff_id group by post order by work_hours desc
1 计算每个人的工作时长,用timestampdiff,以分钟为单位,再除以60得到小时
2 按职位分组,取avg即可,注意三位小数,排序问题