ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

连续上班的天数(剔除周六周日)

2022-01-25 00:03:10  阅读:198  来源: 互联网

标签:no -- 天数 employee 周六 date col 周日


需求 - 计算最大的连续上班天数,剔除掉周六周日

create table temp.temp_fuyun_attendance_tb (
date_col     string,
employee_no  string,
is_punch     string
)
row format delimited fields terminated by '\t';

实现思路 :

需求 - 计算最大的连续上班天数,剔除掉周六周日
思路:
1.求出每日对应的星期

实现代码

//连续登录的天数,跳过周六周日
// 周四 周五 周一 周二  连续四天上班
// 思路
// 1.根据时间添加日期字段

方法一:自定义算法函数

mod(datediff(date_col, '1970-02-01'), 7) week_which_day --标记周几,周日为0

方法二:使用常用dayofweek函数

dayofweek(date_col);


// 2.根据当前的时间 求出来 rownumber 序号,根据当前员工号分组排序,过滤出来上班的和周六周日的
// 3.

with t1_tb as
         (
             select date_col, employee_no, week_which_day,
                    date_sub(date_col, (row_number() over(partition by employee_no order by date_col) - 1)) from_day --连续打卡的开始日期
             from
                 (
                     select date_col, employee_no, is_punch,
                            mod(datediff(date_col, '1970-02-01'), 7) week_which_day --标记周几,周日为0
                     from temp.temp_fuyun_attendance_tb
                     group by date_col, employee_no, is_punch
                 ) t
                 --将缺勤的数据过滤,但保留周末的数据(如果过滤周末数据,则每个员工最大连续打卡天数为5)
             where is_punch = '上班' or week_which_day in (6, 0)
         )

select employee_no, start_date, end_date, continuous_days
from
    (
        select employee_no, start_date, end_date, continuous_days,
               row_number() over(partition by employee_no order by continuous_days desc) rn
        from
            (
                select employee_no,
                       min(date_col) start_date, --连续打卡的开始日期
                       max(date_col) end_date, --连续打卡的结束日期
                       count(1) continuous_days --连续打卡天数
                from t1_tb
                where week_which_day not in (6, 0)
                group by from_day, employee_no
            ) t
    ) t1
where rn = 1 --每个员工只取连续打卡最大天数的记录

需求2:计算最大的连续上班天数,剔除掉周五周六

需求3:计算最大的连续上班天数,并且打印出最大的连续上班天数中的起始打卡日期和结束打开日期

标签:no,--,天数,employee,周六,date,col,周日
来源: https://blog.csdn.net/weixin_53150969/article/details/122677209

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有