题解 | 统计加班员工占比
统计加班员工占比
https://www.nowcoder.com/practice/6c0a521c36e14c7599eaef858f6f8233
select st.department,concat(round(avg(case when timestampdiff(minute,at.first_clockin,at.last_clockin)/60 >9.5 then 1 else 0 end)*100,1),"%") ratio from staff_tb st right join attendent_tb at on st.staff_id=at.staff_id group by department order by ratio desc
统计加班员工占比,通过case when timestampdiff(minute,at.first_clockin,at.last_clockin)/60 >9.5 then 1 else 0 end 对员工加班时长进行处理分类,将加班员工标记为1,未加班员工标记为0,后续通过avg()函数的百分比计算用法计算出加班员工占比,将结果乘以100方便进行后续的百分比转换。使用round()函数进行小数的保留一位:通过字符串拼接将数据转化为百分比形式。两张表的连接通过员工id进行连接。

