博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
考勤问题思路和解决
阅读量:6985 次
发布时间:2019-06-27

本文共 3586 字,大约阅读时间需要 11 分钟。

近期在做一个考勤系统,考勤主要关注的是缺勤、迟到和早退。眼下的打卡控制器能够记录username和打卡时间,用户可能一天打卡多次,也可能一天仅仅打了一次卡,这些情况都须要考虑。

打卡信息都存储在考勤表中,从中要挖掘出一个月内的缺勤人员,迟到人员和早退人员,而且能显示缺勤、迟到和早退的时间。

考勤表

CREATE TABLE [dbo].[kaoqin](	[user_name] [varchar](50) NULL,	[card_time] [datetime] NULL) ON [PRIMARY]GO
插入測试数据

INSERT INTO [master].[dbo].[kaoqin]select '张三', '2014-08-03 09:36:00'union allselect '张三', '2014-08-03 18:10:00'union allselect '张三', '2014-08-04 08:32:00'union allselect '张三', '2014-08-04 15:15:00'union allselect '张三', '2014-08-05 09:32:00'union allselect '张三', '2014-08-05 15:15:00'union allselect '张三', '2014-08-01 08:36:00'union allselect '张三', '2014-08-01 18:10:00'union allselect '张三', '2014-08-02 08:32:00'union allselect '张三', '2014-08-02 18:15:00'union allselect '张三', '2014-08-25 08:00:00'union allselect '张三', '2014-08-24 19:00:00'union allselect '张三', '2014-08-27 08:00:00'union allselect '张三', '2014-08-27 17:00:00'union allselect '张三', '2014-08-26 10:00:00'union allselect '张三', '2014-08-26 18:30:00'union allselect '张三', '2014-08-26 8:00:00'union allselect '张三', '2014-08-27 18:56:00'          GO
我的思路是用一张暂时表得到这个月的全部工作日。将该暂时表与用户进行交叉连接。这样每一个用户在这个月的每一个工作日都有一条记录。

如果早上9点为上班时间,18点为下班时间,这个能够兴许做成变量的形式。

declare @time_start datetimedeclare	@time_end datetime set @time_start = '2014-08-01 00:00:00'set @time_end = DATEADD(M,1,@time_start)-- 一个月的工作日IF object_id('tempdb..#tempDate') is not nullBEGIN	drop table #tempDateENDCREATE table #tempDate(		stat_day varchar(10))IF object_id('tempdb..#tempUserDate') is not nullBEGIN	drop table #tempUserDateENDCREATE table #tempUserDate(			stat_day varchar(10),	[user_name] varchar(40))CREATE clustered index tempUserDate_Index1 on #tempUserDate ([user_name],stat_day)declare @time_temp datetimeset @time_temp = @time_startwhile @time_temp < @time_endbegin   if datepart(weekday,@time_temp)>1 and datepart(weekday,@time_temp)<7   begin	   insert into #tempDate (stat_day) values (CONVERT(varchar(10),@time_temp,121))   end   set @time_temp= dateadd(d,1,@time_temp)endinsert into #tempUserDateselect * from #tempDate  cross join(select distinct [user_name] from [kaoqin]) t
从原始的kaoqin表中查询出每一个用户的上班时间和下班时间。假设用户一天的打开记录超过两条。那么就会取最早和最晚的一条分别作为上班时间和下班时间。

select [user_name],CONVERT(varchar(10),card_time,121) as stat_day,    MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin]    group by [user_name],CONVERT(varchar(10),card_time,121)
通过暂时表#tempUserDate和上面的查询结果关联,假设左联接为空,则证明该人员缺勤。

--缺勤select * from #tempUserDate aleft join(    select [user_name],CONVERT(varchar(10),card_time,121) as stat_day,    MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin]    group by [user_name],CONVERT(varchar(10),card_time,121)) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_daywhere [b].[user_name] is null
以下是迟到和早退的实现SQL。

--迟到select * from #tempUserDate aleft join(    select [user_name],CONVERT(varchar(10),card_time,121) as stat_day,    MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin]    group by [user_name],CONVERT(varchar(10),card_time,121)) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_daywhere CONVERT(varchar(100), [b].[on_time], 8)>'09:00:00'--早退select * from #tempUserDate aleft join(    select [user_name],CONVERT(varchar(10),card_time,121) as stat_day,    MIN(card_time) as on_time,MAX(card_time) as off_time from [kaoqin]    group by [user_name],CONVERT(varchar(10),card_time,121)) b on a.[user_name]=b.[user_name] and a.stat_day=b.stat_daywhere CONVERT(varchar(100), [b].[off_time], 8)<'18:00:00'
得到的结果

假设某个人他今天既迟到又早退在终于的结果中都会体现。能够从2014-08-05这条数据看出。当然,这个考勤系统还不完好,比如没有将节日考虑进来,初步的考虑是採用Job定期存储每年的节日,假设员工请假,也须要纳入到系统的考虑中。

你可能感兴趣的文章
矩阵:如何使用矩阵操作进行 PageRank 计算?
查看>>
阿里云储道深度解析存储系统设计——NVMe SSD性能影响因素一探究竟
查看>>
2016:编写高性能的JavaScript
查看>>
[算法]-将功赎过(js求二进制的两种方式)
查看>>
探索前端黑科技——通过 png 图的 rgba 值缓存数据
查看>>
总结移动开发实践中遇到的坑
查看>>
极客学院#2:HTML5
查看>>
【友盟+】推送Android SDK 3.0发布,一次拯救消息到达率的迭代
查看>>
梯度下降法变种的汇总
查看>>
技术往事:改变世界的TCP/IP协议(珍贵多图、手机慎点)
查看>>
安装Xcache,配置管理页面
查看>>
Git常用命令和Git团队使用规范指南
查看>>
swift 监测网络状态
查看>>
那是我夕阳下的奔跑 —— Gemini
查看>>
一个不错的抛物线js效果
查看>>
优麒麟 16.04.6 LTS 版本发布!
查看>>
Ant Motion 1.7.0 发布,React 框架动效解决方案
查看>>
如何解决不能使用Process的Start方法运行一个程序的问题。
查看>>
centos6 简单编译安装 php5.4
查看>>
阿里云RDS PostgreSQL GPU加速规格(支持GIS时空加速)发布 ...
查看>>