PostgreSQL下如何统计员工连续5天及以上未出勤天数
PostgreSQL 考勤连续未出勤统计场景
测试表结构与示例数据
本次场景共涉及3张核心业务表,所有数据均为测试用模拟考勤数据:
1. 排班规则配置表 resource_calendar_attendance
存储不同日历下每日的上下班时间、时段划分规则,建表与示例数据如下:
CREATE TABLE resource_calendar_attendance ( id integer NOT NULL, name character(256), hour_from double precision, hour_to double precision, calendar_id integer, day_period character(256) ); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(1, 'Monday Morning', 8, 12, 1, 'morning'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(2, 'Monday Evening', 8, 13, 17, 'afternoon'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(3, 'Tuesday Morning', 8, 12, 1, 'morning'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(4, 'Tuesday Evening', 8, 13, 17, 'afternoon'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(5, 'Wednesday Morning', 8, 12, 1, 'morning'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(6, 'Wednesday Evening', 8, 13, 17, 'afternoon'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(7, 'Thursday Morning', 8, 12, 1, 'morning'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(8, 'Thursday Evening', 8, 13, 17, 'afternoon'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(9, 'Friday Morning', 8, 12, 1, 'morning'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(10, 'Friday Evening', 8, 13, 17, 'afternoon'); INSERT INTO resource_calendar_attendance(id, name, hour_from, hour_to, calendar_id, day_period) VALUES(11, 'Saturday Morning', 8, 12, 1, 'morning');
2. 排班日历主表 resource_calendar
存储不同类型的排班日历基础信息,建表与示例数据如下:
CREATE TABLE resource_calendar ( id integer NOT NULL, name character(256) ); INSERT INTO resource_calendar(id, name) VALUES(1, '标准每周40小时工作制');
3. 员工考勤打卡表 hr_attendance
存储员工每日的上下班打卡时间、所属排班日历信息,建表与示例数据如下:
CREATE TABLE hr_attendance ( id integer NOT NULL, calendar_id integer NOT NULL, employee_name character(256), check_in timestamp, check_out timestamp ); INSERT INTO hr_attendance(id, calendar_id, employee_name, check_in, check_out) VALUES(1, 1, 'Jame', '2020-09-01 07:00:00', '2020-09-01 18:00:00'); INSERT INTO hr_attendance(id, calendar_id, employee_name, check_in, check_out) VALUES(2, 1, 'Alex', '2020-09-01 07:30:00', '2020-09-01 17:45:00'); INSERT INTO hr_attendance(id, calendar_id, employee_name, check_in, check_out) VALUES(3, 1, 'Jame', '2020-09-07 07:12:00', '2020-09-07 18:22:00'); INSERT INTO hr_attendance(id, calendar_id, employee_name, check_in, check_out) VALUES(4, 1, 'Alex', '2020-09-02 07:07:00', '2020-09-02 18:08:00'); INSERT INTO hr_attendance(id, calendar_id, employee_name, check_in, check_out) VALUES(5, 1, 'Alex', '2020-09-03 06:13:00', '2020-09-03 18:24:00');
统计需求与判定示例
核心需求:筛选统计连续5个及以上规定工作日未出勤的员工名单。
以员工Jame2020年9月的考勤记录为例,判定过程如下:
员工Jame 9月考勤逐天核对记录:
- 2020-09-01:正常打卡,出勤有效
- 2020-09-02:无打卡记录
- 2020-09-03:无打卡记录
- 2020-09-04:无打卡记录
- 2020-09-05:无打卡记录
- 2020-09-06:周末非工作日,不计入缺勤统计
- 2020-09-07:无打卡记录
- 2020-09-08:正常打卡,出勤有效
按照规则核算,Jame的连续缺勤时长达5天以上,属于符合统计条件的对象。
内容的提问来源于stack exchange,提问作者Xuan Thanh Tran
相关产品推荐
相关产品推荐

