首页 > 试题广场 >

统计加班员工占比

[编程题]统计加班员工占比
  • 热度指数:34416 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
某公司员工信息数据及单日出勤信息数据如下:
员工信息表staff_tb(staff_id-员工id,staff_name-员工姓名,staff_gender-员工性别,post-员工岗位类别,department-员工所在部门),如下所示:
staff_id staff_name staff_gender post department
1 Angus male Financial dep1
2 Cathy female Director dep1
3 Aldis female Director dep2
4 Lawson male Engineer dep1
5 Carl male Engineer dep2
6 Ben male Engineer dep1
7 Rose female Financial dep2

出勤信息表attendent_tb(info_id-信息id,staff_id-员工id,first_clockin-上班打卡时间,last_clockin-下班打卡时间),如下所示:
info_id staff_id first_clockin last_clockin
101 1 2022-03-22 08:00:00 2022-03-22 17:00:00
102 2 2022-03-22 08:30:00 2022-03-22 18:00:00
103 3 2022-03-22 08:45:00 2022-03-22 17:00:00
104 4 2022-03-22 09:00:00 2022-03-22 18:30:00
105 5 2022-03-22 09:00:00 2022-03-22 18:10:00
106 6 2022-03-22 09:15:00 2022-03-22 19:30:00
107 7 2022-03-22 09:30:00
2022-03-22 18:29:00

问题:请统计该公司各部门加班员工所占比例?
注:工作时长大于9.5小时定义为加班
要求输出:部门(department)、加班员工占比(ratio,以百分数形式输出并保留1位小数),查询结果按照加班员工占比降序排序;
示例数据结果如下:
department ratio
dep1 25.0%
dep2 0.0%
解释:dep1部门共有4名员工,其中仅有员工6(Ben)在该日加班,所以结果为1/4=25.0%
其他结果同理.....
示例1

输入

drop table if exists  `staff_tb` ; 
CREATE TABLE `staff_tb` (
`staff_id` int(11) NOT NULL,
`staff_name` varchar(16) NOT NULL,
`staff_gender` char(8) NOT NULL,
`post` varchar(11) NOT NULL,
`department` varchar(16) NOT NULL,
PRIMARY KEY (`staff_id`));
INSERT INTO staff_tb VALUES(1,'Angus','male','Financial','dep1'); 
INSERT INTO staff_tb VALUES(2,'Cathy','female','Director','dep1'); 
INSERT INTO staff_tb VALUES(3,'Aldis','female','Director','dep2'); 
INSERT INTO staff_tb VALUES(4,'Lawson','male','Engineer','dep1'); 
INSERT INTO staff_tb VALUES(5,'Carl','male','Engineer','dep2'); 
INSERT INTO staff_tb VALUES(6,'Ben','male','Engineer','dep1'); 
INSERT INTO staff_tb VALUES(7,'Rose','female','Financial','dep2'); 

drop table if exists  `attendent_tb` ;   
CREATE TABLE `attendent_tb` (
`info_id` int(11) NOT NULL,
`staff_id` int(11) NOT NULL,
`first_clockin` datetime NULL,
`last_clockin` datetime NULL,
PRIMARY KEY (`info_id`));
INSERT INTO attendent_tb VALUES(101,1,'2022-03-22 08:00:00','2022-03-22 17:00:00');
INSERT INTO attendent_tb VALUES(102,2,'2022-03-22 08:30:00','2022-03-22 18:00:00');
INSERT INTO attendent_tb VALUES(103,3,'2022-03-22 08:45:00','2022-03-22 17:00:00');
INSERT INTO attendent_tb VALUES(104,4,'2022-03-22 09:00:00','2022-03-22 18:30:00');
INSERT INTO attendent_tb VALUES(105,5,'2022-03-22 09:00:00','2022-03-22 18:10:00');
INSERT INTO attendent_tb VALUES(106,6,'2022-03-22 09:15:00','2022-03-22 19:30:00');
INSERT INTO attendent_tb VALUES(107,7,'2022-03-22 09:30:00','2022-03-22 18:29:00');

输出

department|ratio
dep1|25.0%
dep2|0.0%
头像 牛客题解官
发表于 2025-02-25 15:55:08
精华题解 这道题目要求我们统计公司各部门加班员工所占比例。加班定义为工作时长大于9.5小时。我们需要从员工信息和出勤信息中提取数据,计算每个部门的加班比例,并按比例降序排序。下面我将详细讲解这个SQL查询的思路和实现步骤。 题目描述 我们有两个表: staff_tb:包含员工信息,包括员工ID、姓名、性别、 展开全文
头像 牛客264897283号
发表于 2025-03-09 08:28:35
select a.department, concat ( round( sum( if ( timestampdiff (second, b.first_clockin, 展开全文
头像 想减肥的本杰明在许愿
发表于 2025-02-21 16:11:38
/*按部门分组,百分数,保留1位,降序*/ with t1 as ( select staff_id, if( timestampdiff (second, first_clockin, last_clockin) / 3600 > 9.5,1,0) as is_time from 展开全文
头像 牛客690839070号
发表于 2025-05-10 14:52:55
select department,concat(round(sum(if(timestampdiff(second,first_clockin,last_clockin)/60/60>9.5,1,0))/count(distinct b.staff_id)*100,1),'%') ratio 展开全文
头像 怕黑的小龙虾最喜欢春天
发表于 2025-06-26 16:14:59
select st.department, concat(round(count(first_clockin) / count(*) * 100,1) , '%') as ratio from staff_tb st left join attendent_tb at on st.staf 展开全文
头像 在思考的六边形战士很想去旅行
发表于 2025-08-01 16:26:40
# 查询后面各种函数嵌套得有点多而已,计算思路清晰的话其实还好 # 计算间隔时长多少个小时,timestampdiff以分钟为单位再除以60,这样相对准确 # 以小时为单位,会出现因取舍导致的误差 select s.department, concat( round( 展开全文
头像 夸克__
发表于 2025-10-23 09:29:32
select s.department, concat( round( sum( case when timestampdiff(second, first_clockin 展开全文
头像 五首绝句哈
发表于 2025-06-18 19:55:20
SELECT department,concat(format(avg(CASE WHEN duration > 570 THEN 1 ELSE 0 END)*100,1),'%') AS ratio FROM( SELECT a.staff_id,a.department,r 展开全文
头像 纯粹爱刷题294408346
发表于 2025-03-16 11:06:44
WITH attendent AS( SELECT staff_id, if( TIMESTAMPDIFF(second,first_clockin,last_clockin)/3600>9.5, #需要使用secound-&g 展开全文
头像 牛客990942309号
发表于 2025-03-22 04:15:49
SELECT department, CONCAT(ROUND( 100 * SUM(TIMESTAMPDIFF(SECOND, first_clockin, last_clockin)/3600 > 9.5) / COUNT(DISTINC 展开全文
头像 __hello__world
发表于 2025-03-01 23:13:22
select department, concat (round(100 * avg(is_time), 1), '%') as ratio from ( select staff_id, if ( 展开全文