SQL 统计每日上班打卡和下班打卡语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 统计每日上班打卡和下班打卡语句相关的知识,希望对你有一定的参考价值。
有以上员工打卡记录表
其中
Card_No为考勤卡号
Atte_time 为打卡时间
DoorInout为上班或下班,1为上班,0位下班
想统计没人每天最早上下班打卡,和其他上下班打卡 各去4笔 如不足则为空
如以下样式的表
-----------------------------------------
考勤卡号 日期 上班打卡 上班打卡1 上班打卡2 上班打卡3 下班打卡 下班打卡1 下班打卡2 下班打卡310890895 2013-08-10 16:04 20:24 NULL NULL 19:43 2013-08-11 00:46 NULL NULL10890895 2013-08-11 16:04 19:45 20:18 NULL NULL NULL NULL NULL
如果下班在第二天3点以内的也统计入下班打卡中下班打卡早于最早上班打卡的数据忽略
CREATE TABLE AtteTime
(
Card_No VARCHAR(10) ,
Atte_Time DATETIME ,
DoorInOut BIT
)
select A1.card_no,
A1.atte_time as "上班时间",
A2.atte_time as "上班时间1",
A3.atte_time as "上班时间2",
A4.atte_time as "上班时间3",
B1.atte_time as "下班时间",
B2.atte_time as "下班时间1",
B3.atte_time as "下班时间2",
B4.atte_time as "下班时间3"
from (select card_no, atte_time
from (select card_no,
atte_time,
ROW_NUMBER() over(partition by card_no order by atte_time) as In_ID
from attetime
where doorinout = 1) T
where T.In_ID = 1) A1
left join (select card_no, atte_time
from (select card_no,
atte_time,
ROW_NUMBER() over(partition by card_no order by atte_time) as In_ID
from attetime
where doorinout = 1) T
where T.In_ID = 2) A2
on A1.card_no = A2.card_no
left join (select card_no, atte_time
from (select card_no,
atte_time,
ROW_NUMBER() over(partition by card_no order by atte_time) as In_ID
from attetime
where doorinout = 1) T
where T.In_ID = 3) A3
on A1.card_no = A3.card_no
left join (select card_no, atte_time
from (select card_no,
atte_time,
ROW_NUMBER() over(partition by card_no order by atte_time) as In_ID
from attetime
where doorinout = 1) T
where T.In_ID = 4) A4
on A1.card_no = A4.card_no
full join (select card_no, atte_time
from (select card_no,
atte_time,
ROW_NUMBER() over(partition by card_no order by atte_time desc) as Out_ID
from attetime
where doorinout = 0) T
where T.Out_ID = 1) B1
on A1.card_no = B1.card_no
left join (select card_no, atte_time
from (select card_no,
atte_time,
ROW_NUMBER() over(partition by card_no order by atte_time desc) as Out_ID
from attetime
where doorinout = 0) T
where T.Out_ID = 2) B2
on A1.card_no = B2.card_no
left join (select card_no, atte_time
from (select card_no,
atte_time,
ROW_NUMBER() over(partition by card_no order by atte_time desc) as Out_ID
from attetime
where doorinout = 0) T
where T.Out_ID = 3) B3
on A1.card_no = B3.card_no
left join (select card_no, atte_time
from (select card_no,
atte_time,
ROW_NUMBER() over(partition by card_no order by atte_time desc) as Out_ID
from attetime
where doorinout = 0) T
where T.Out_ID = 4) B4
on A1.card_no = B4.card_no
投入验证数据如下:
执行结果如下:
参考技术A 这是在oracle数据库下做的,不知道你是什么数据库,给你提供一下思路select
card_no,
attr_date,
to_char(atte_time,'HH24:MI')tim,
doorinout,
rn
from (
select
card_no,
attr_date,
atte_time,
doorinout,
ROW_NUMBER() OVER(PARTITION BY card_no,attr_date,doorinout ORDER BY atte_time asc) rn
from(
select
card_no,
(
case
when doorinout=1
then to_date(to_char(atte_time,'yyyy-MM-dd'),'yyyy-MM-dd')
when (doorinout=0 and to_number(to_char(atte_time,'HH24'))<3)
then (to_date(to_char(atte_time,'yyyy-MM-dd'),'yyyy-MM-dd')-1)
else to_date(to_char(atte_time,'yyyy-MM-dd'),'yyyy-MM-dd')
end
)attr_date,
atte_time,
doorinout
from AtteTime
) tmp order by card_no,atte_time asc,doorinout desc
) where rn < 5 参考技术B 能不能来一份数据 这么凭空想 或者构造数据 太麻烦 一些代表性的数据 就行。还有问题要描述清楚 :1)各 取 4笔还是各 去 4 笔,少了为空,多了呢?取时间早的还是时间晚的。
2) 没人每天是什么意思
3) 不经过调试 谁也不能保证自己写出的语句是100%正确的,给点代表性的数据。追问
上班,下班各去4笔数据,少了留空,多了去出去最早前4笔
每人每天的数据意思就是按照考勤号和日期进行分组
数据:字数限制放不上来
给分代表性的数据 错别字还是少点好
追问数据字数太长了放不上来,百度HI上给你吧.
SQL如何判断打卡记录是不是异常?
公司有考勤表数据,记录了人员信息及打卡时间,要怎么来判断打卡时间是否异常?
目前打卡分为上午上班(7:30-8:00),上午下班(12:00-12:30),下午上班(13:30-14:00),下午下班(18:00-18:30)四个阶段,打卡记录在这四个时间段内为正常打卡,其他时间段打卡为异常记录。
这里有两个问题点想请教下大神:
1、首先需要判断当前打卡日期是否在正常工作日内;
2、同一时间段重复打卡算一次有效打卡;
感谢!!!
case when
日期字段 in (between 7.30 and 8.00,between 12.00 and 12.30,between 13.30 and 14.00,between 18 and 18.30)then ‘正常’ else ‘异常’ end,
大概就是这么写吧,这个字段,把时间字段处理一下应该就行
参考技术A是否是正常工作日,要看你们想怎么定义,是自定义默认周末还是根据国务院发布的全年的假期安排。两种方案:①简单计算,就按照周末算。程序写就能完成。②把节假日单独入库存表或者调用百度日历API判断。
打卡只取第一次打卡,程序判断是否打卡,时间段是否已存在打卡记录等。重复打卡入库的话就只取时间段内最大的打卡时间。具体看业务。
以上是关于SQL 统计每日上班打卡和下班打卡语句的主要内容,如果未能解决你的问题,请参考以下文章
从 SQL Server 中的多个打卡或多个打卡中获取打卡和打卡时间
每日SQL打卡DAY 13丨每日新用户统计难度中等