一个关于考勤统计的sql研究
在这里,我们要做一个简单的员工考勤记录查询系统的后台数据库。业务需求如下所示:
解决这个问题的时候本来考虑的是在考勤信息记录表中按照日期对考勤信息进行分组,然后取每组中上班时间(att_work_datatime)的最小值,但是后来几经折腾发现group by只能实现分组,而order by只能实现组外排序,因此这个方法只能放弃。再三考虑了一下,可以在分组之前先对表中att_work_datatime列中的所有值进行升序排序后生成一个临时表,然后对这个临时表中的att_work_datatime按照日期再分组,这样对att_work_datatime列按照日期group by之后取的就是每天上班打卡最早的人,我们从attendance_info_table(考勤信息表)表中查询出出每天上班时间最早的人的编号、上班时间和下班时间,sql语句如下:
from

order by att_work_datatime) as tmp

from employee_info_table as em ,
(select id,att_work_datatime,after_work_datatime,emp_id
from attendance_info_table
where att_work_datatime is not null
order by att_work_datatime )
as tmp
where em.id=tmp.emp_id
group by Date(att_work_datatime)
select eit.*, ait.att_work_datatimefrom attendance_info_table ait, employee_info_table eitwhere ait.emp_id = eit.id and ait.att_work_datatime in(select min(att_work_datatime)from attendance_info_tablewhere att_work_datatime is not nullgroup by Date(att_work_datatime))order by ait.att_work_datatime asc;
from employee_info_table as em ,
(select id,att_work_datatime,after_work_datatime,emp_id
from attendance_info_table
where att_work_datatime is not null
order by att_work_datatime desc)
as tmp
where em.id=tmp.emp_id
group by Date(att_work_datatime)
select eit.*, ait.att_work_datatimefrom attendance_info_table ait, employee_info_table eitwhere ait.emp_id = eit.id and ait.att_work_datatime in(select max(att_work_datatime)from attendance_info_tablewhere att_work_datatime is not nullgroup by Date(att_work_datatime))order by ait.att_work_datatime asc;
步骤和2.1中统计每天来的最早的人的方法相同,唯一不同的是对表中列中after_work_datatime的所有值进行升
序排序,并将查询的列由att_work_datatime改为after_work_datatime,sql语句如下:
from employee_info_table as em ,
(select id,att_work_datatime,after_work_datatime,emp_id
from attendance_info_table
where after_work_datatime is not null
order by after_work_datatime)
as tmp
where em.id=tmp.emp_id
group by Date(after_work_datatime)
select eit.*, ait.after_work_datatimefrom attendance_info_table ait, employee_info_table eitwhere ait.emp_id = eit.id and ait.after_work_datatime in(select min(after_work_datatime)from attendance_info_tablewhere after_work_datatime is not nullgroup by Date(after_work_datatime))order by ait.after_work_datatime asc;
from employee_info_table as em ,
(select id,att_work_datatime,after_work_datatime,emp_id
from attendance_info_table
where after_work_datatime is not null
order by after_work_datatime desc)
as tmp
where em.id=tmp.emp_id
group by Date(after_work_datatime)
select eit.*, ait.after_work_datatimefrom attendance_info_table ait, employee_info_table eitwhere ait.emp_id = eit.id and ait.after_work_datatime in(select max(after_work_datatime)from attendance_info_tablewhere after_work_datatime is not nullgroup by Date(after_work_datatime))order by ait.after_work_datatime asc;
from
(select
id,att_work_datatime,after_work_datatime,timediff(after_work_datatime,att_work_datatime)
as att_time,emp_id
select eit.*,tmp.att_time, tmp.att_work_datatimefrom employee_info_table eit,(select id,att_work_datatime,timediff(after_work_datatime,att_work_datatime) as att_time,emp_idfrom attendance_info_tablewhere att_work_datatime is not null and after_work_datatime is not null) as tmpwhere eit.id=tmp.emp_id and tmp.att_time in(select max(timediff(after_work_datatime,att_work_datatime)) as att_timefrom attendance_info_tablewhere att_work_datatime is not null and after_work_datatime is not nullgroup by date(att_work_datatime))group by date(tmp.att_work_datatime)order by att_work_datatime;执行出的结果如下图所示:
select eit.*,tmp.att_time, tmp.att_work_datatimefrom employee_info_table eit,(select id,att_work_datatime,timediff(after_work_datatime,att_work_datatime) as att_time,emp_idfrom attendance_info_tablewhere att_work_datatime is not null and after_work_datatime is not null) as tmpwhere eit.id=tmp.emp_id and tmp.att_time in(select min(timediff(after_work_datatime,att_work_datatime)) as att_timefrom attendance_info_tablewhere att_work_datatime is not null and after_work_datatime is not nullgroup by date(att_work_datatime))group by date(tmp.att_work_datatime)order by att_work_datatime;执行结果如下所示:
select date(att_work_datatime) as date,count(*) as late_numsfrom attendance_info_tablewhere timediff(time(att_work_datatime),'09:30:59') > 0 and att_work_datatime is not nullgroup by date(att_work_datatime)
select date(after_work_datatime) as date,count(*) as leave_early_numsfrom attendance_info_tablewhere after_work_datatime is not nulland timediff(time(after_work_datatime),'18:00:00')<0or timediff(after_work_datatime,att_work_datatime)<'08:00:00'group by date(after_work_datatime)执行结果如下图所示:
select eit.*, count(*) as late_numsfrom attendance_info_table as ait,employee_info_table as eitwhere ait.att_work_datatime is not nulland timediff(time(ait.att_work_datatime),'09:30:59') > 0and ait.emp_id = eit.idgroup by emp_idorder by late_nums desc;

select eit.*,timediff(time(ait.att_work_datatime),'09:30:59') as lately_times,date(ait.att_work_datatime) as lately_datefrom attendance_info_table as ait,employee_info_table as eitwhere ait.att_work_datatime is not nulland timediff(time(ait.att_work_datatime),'09:30:59') > 0and eit.id=ait.emp_idorder by eit.emp_name asc;执行结果如下:
select
eit.*,timediff(time(ait.att_work_datatime),'09:30:59') as
lately_times,date(ait.att_work_datatime) as
lately_date,(10+1*(TIME_TO_SEC(timediff(time(ait.att_work_datatime),'09:30:59')))/60)
as '罚金(元)'
from attendance_info_table as ait,employee_info_table as eit
where ait.att_work_datatime is not null
and timediff(time(ait.att_work_datatime),'09:30:59') > 0
and eit.id=ait.emp_id
order by eit.emp_name asc
执行结果如下:
6.1计算出公司每天因为迟到所扣的钱select tmp.lately_date,sum(tmp.fadefor) as '总罚金(元)'
from
(select eit.*,timediff(time(ait.att_work_datatime),'09:30:59') as
lately_times,date(ait.att_work_datatime) as
lately_date,(10+1*(TIME_TO_SEC(timediff(time(ait.att_work_datatime),'09:30:59')))/60)
as fadefor
from attendance_info_table as ait,employee_info_table as eit
where ait.att_work_datatime is not null
and timediff(time(ait.att_work_datatime),'09:30:59') > 0
and eit.id=ait.emp_id
order by eit.emp_name asc) as tmp
group by tmp.lately_date执行结果如下:
select tmp.id,tmp.emp_name,sum(tmp.fadefor) as 'total_fadefor' from
(select eit.*,(10+1*(TIME_TO_SEC(timediff(time(ait.att_work_datatime),'09:30:59')))/60) as 'fadefor'
from attendance_info_table as ait,employee_info_table as eit
where ait.att_work_datatime is not null
and timediff(time(ait.att_work_datatime),'09:30:59') > 0
and eit.id=ait.emp_id) as tmp
group by tmp.id
order by total_fadefor desc;


(select eit.*,count(*) as normal_nums
from attendance_info_table as ait,employee_info_table as eit
where ait.att_work_datatime is not null
and ait.after_work_datatime is not null
and timediff(time(ait.att_work_datatime),'09:30:59') < 0
and timediff(after_work_datatime,att_work_datatime)>'08:00:00'
and ait.emp_id = eit.id
group by ait.emp_id
)as tmp where tmp.normal_nums>=21

(select eit.*,count(*) as normal_nums
from attendance_info_table as ait,employee_info_table as eit
where ait.att_work_datatime is not null
and ait.after_work_datatime is not null
and timediff(time(ait.att_work_datatime),'09:30:59') < 0
and timediff(after_work_datatime,att_work_datatime)>'08:00:00'
and ait.emp_id = eit.id
group by ait.emp_id
)as tmp
where tmp.normal_nums>=
(select count(*)
from
(select date(att_work_datatime) as date
from attendance_info_table
where att_work_datatime is not null
group by date(att_work_datatime)) as tmp)
一个关于考勤统计的sql研究的更多相关文章
- mysql统计类似SQL语句查询次数
mysql统计类似SQL语句查询次数 vc-mysql-sniffer 工具抓取的sql分析. 1.先用shell脚本把所有enter符号替换为null,再根据语句前后的字符分隔语句 grep -Ev ...
- 考勤输入导入OA平台与考勤统计报表导出功能源代码
注:以某某公司为例,每日签到时间为8点整 每日签退时间为17点30分 规则:公司签到签退时间在OA平台中可以视实际情况调整,当天有请假并通过工作流审批通过为有效,当天因公外出并通过工作流审批通过为 ...
- c#考勤统计
现在项目需求,需要从多张表中获取数据,组装到一个实体对象中,并通过计算统计出每个员工的考勤记录.(全凭自己思考做的,不足的地方希望各位大神指正!毕竟自己能力有限,思考不全) 考勤统计列表: 明细列表: ...
- 如果一条SQL语句太长,我们可以通过回车键来创建一个新行来编写SQL语句,SQL语句的命令结束符为分号(;)。
1.如果一条SQL语句太长,我们可以通过回车键来创建一个新行来编写SQL语句,SQL语句的命令结束符为分号(;). 2.select查询的多个字段之间要用逗号“,”分割,如果查询涉及多个表,那多个表之 ...
- --投资情况统计详情sql
--投资情况统计详情sqlselect BidRecord.*, RegInfo.UserName,UserInfo.phone,BorrowInfo.Title,BorrowInfo.BorrowC ...
- 写一个程序,统计自己C语言共写了多少行代码,Github基本操作
前言 在上一篇博客中,本人提到了自己的文件操作可以说是几乎没用过.现在想想,这也算是只在OJ上做题的一个弊端吧.虽然通过OJ做题是一个学习代码好手段,但其他方面也要多多涉猎才好,而不是说OJ用不到文件 ...
- 已知一个字符串S 以及长度为n的字符数组a,编写一个函数,统计a中每个字符在字符串中的出现次数
import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/6 21:04 * @description ...
- 如何用java完成一个中文词频统计程序
要想完成一个中文词频统计功能,首先必须使用一个中文分词器,这里使用的是中科院的.下载地址是http://ictclas.nlpir.org/downloads,由于本人电脑系统是win32位的,因此下 ...
- 核心API的使用(给定一个字符串,统计每个字符出现的次数)
/** * 给定一个字符串,统计每个字符出现的次数. 如:abdaewrwqask435a1aasd */public class ReplaceString { static int length; ...
随机推荐
- 《JAVA---day03---运算符》
/*[位运算符] 运算符: 运算 范例 说明 <<: 左移 3<<2=12--->3*2*2=12 >>: 右移 3>>2=1---->3/ ...
- Beta 冲刺(6/7)
前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10129063.html 作业博客:https://edu.cnblogs.com/campus ...
- utf-8和Unicode的区别
链接 utf-8和Unicode到底有什么区别?是存储方式不同?编码方式不同?它们看起来似乎很相似,但是实际上他们并不是同一个层次的概念 要想先讲清楚他们的区别,首先应该讲讲Unicode的来由. 众 ...
- Python学习(002)--Python介绍
一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...
- Java实现哈夫曼编码和解码
最近无意中想到关于api返回值加密的问题,譬如我们的api需要返回一些比较敏感或者重要不想让截获者得到的信息,像如果是做原创图文的,文章明文返回的话则有可能被抓包者窃取. 关于请求时加密的方式比较多, ...
- magento开发中文手册
Magento开发 第一章 手册简介Introduction 对一个开发人员来说,电子商务开发也许是现今最具创造性的工作.在这个瞬息万变的网络世界,为了保持始终领先竞争对手一步,无论是对你自己还是你的 ...
- CentOS7安装OpenStack(Rocky版)-03.安装Glance镜像服务组件(控制节点)
上篇文章分享了keystone的安装配置,本文接着分享openstack的镜像服务glance. --------------- 完美的分割线 ---------------- 3.0.glance概 ...
- Docker教程-01.安装docker-ce-18.06
参考文章:http://www.runoob.com/docker/docker-tutorial.html 1.Docker简介 1)Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 ...
- PHP CURL HTTPS内存泄露问题
还原场景:通过一直运行脚本,向微信用户发送模板消息,发现运行了一段时间,内存就爆了,然后立马看了一下代码,发现跟其他的消息逻辑一模一样,唯一不一样的就是请求了微信的接口:然后继续开始找问题,发现当时使 ...
- Dataframe 新增一列, apply 通用方法
df['c'] = df.apply(lambda row: 1 if row['a'] < 0 and row['b'] > 0 else 0, axis=1) apply 是一个好方法 ...


