SQL语言考勤打卡记录

来源:百度知道 编辑:UC知道 时间:2024/06/24 07:29:48
ID card_time
--------------------------------
a 2008-07-15 07:42:12.000
a 2008-07-16 07:47:52.000
a 2008-07-17 07:59:12.000
a 2008-07-18 07:08:37.000
a 2008-07-19 07:05:47.000
a 2008-07-20 07:25:36.000
b 2008-07-15 07:22:12.000
b 2008-07-16 07:05:52.000
b 2008-07-17 07:20:36.000
b 2008-07-18 08:05:47.000
b 2008-07-20 07:25:36.000
c 2008-07-15 07:07:12.000
c 2008-07-16 07:33:52.000
c 2008-07-17 08:01:12.000
c 2008-07-18 07:08:37.000
c 2008-07-19 08:05:22.000
c 2008-07-20 09:25:36.000

以上是一张员工考勤表,8点钟以后视为迟到,9点以后或者没有没有打卡记录的视为旷工,
统计结果:

ID 迟到次数 旷工次数 总的打卡次数
a 0 0 6
b 1 1 5
c 1 1 6

用SQL语言怎么写?

首先要有一个工厂日历的表,列出所有工作日,至少一个字段:工作日 varchar(10)。
然后这样即可:
select id,
迟到次数=sum(case when timec>'08:00:00' tand timec<'09:00:00' then 1 else 0 end),
旷工次数=sum(case when timec>'09:00:00' or timec is null then 1 else 0 end),
打卡次数=sum(case when timec is null then 0 else 1 end)
from
(
SELECT * FROM 工厂日历 left join
(select id,
datec=convert(varchar(10),card_time,120),
timec=substring(convert(varchar,card_time,120),12,8)
from tablename
) a
on 工作日=DATEC
) b
group by ID

假设这个表的名称叫做 work,来存储打卡的时间信息
select ID,
sum(case when card_time>='%-%-% 08:00:00' and card_time<='%-%-% 08:59:59' then 1 else 0 end ) as '迟到次数',
sum(case when card_time>='%-%-% 09:00:00' or card_time='' then 1 else 0 end ) as '旷工次数',
sum(case when card_time<>'&#