sql 考勤数据 未打卡的日期问题

作者&投稿:蔽裴 (若有异议请与网页底部的电邮联系)
sql查询未打卡的日期~

问题补充:我想知道的是一整天都没有打卡的记录。

当然,如果把少了某个时间段的打卡记录SQL语句给我,我会更开心的。
------------------
以下语句已实现了.先生成时间,再比较

if object_id('Tempdb..#') is not null
drop table #
select top 31 identity(int,0,1) as ID,cast(null as Datetime)as Date
into #
from syscolumns

declare @Date datetime
set @Date='2009-07-01'

update # set Date=dateadd(dd,ID,@Date)

select
t1.员工ID,t2.Date as 日期
from (select 员工ID,Date from (select distinct 员工ID from 考勤表) as a,# as b)t1
left join 考勤表 t2 on datediff(dd,t1.Date,T2.Date)=0 and t1.员工ID=t2.员工ID
where t1.员工ID is null

oracle:
select 考勤号,日期,wm_concat(时间) from 表 group by 考勤号,日期
sql:
select stuff((select ',' + 时间
from 表
where 日期 = a.日期
and 考勤卡号 = a.考勤卡号
for XML path('')),
1,
1,
'') 工号,
考勤卡号,
日期
From 表 a
group by 考勤卡号, 日期

第一个是sqlserver的,第二个是oracle的,我这里建的t5是因为没有完整的表记录,取不到完整的时间,而你这里的表有完整的记录,所以select distinct substring(start_time,0,11) dt,plan_id,cardno from t5这句完全可以select distinct substring(start_time,0,11) dt,plan_id,cardno from tabname(tabname就是你上面那张表)来替代t5,这样可以取出完整的时间集合,总不会出现所有人上班时间都没来吧,呵呵。

create table t5
(start_time varchar(20),plan_id varchar(1),cardNo varchar(10));
create table t4
(
       attend_id varchar(4),plan_id varchar(1),cardNo varchar(10),start_time varchar(20),end_time varchar(20)
);

insert into t4
select '4613','5','00009','2012-09-11 08:37',null
union all
select '4614','5','00009','2012-09-13 08:53','2012-09-13 18:01' 
union all
select '4615','5','00009','2012-09-15 08:54','2012-09-15 18:02'
union all
select '4616','5','00009','2012-09-17 08:30','2012-09-17 18:03'
union all
select '4617','5','00009','2012-09-18 08:37','2012-09-18 19:45'
union all
select '4618','5','00009','2012-09-19 08:37','2012-09-19 19:01'
union all
select '4619','5','00009','2012-09-20 08:37','2012-09-20 18:45'
union all
select '4620','5','00009','2012-09-21 08:37',null
union all
select '4621','5','00009','2012-09-23 08:37','2012-09-23 18:35';

insert into t5
select '2012-09-11 08:37','5','00009'
union all
select '2012-09-12 08:37','5','00009'
union all
select '2012-09-13 08:37','5','00009'
union all
select '2012-09-14 08:37','5','00009'
union all
select '2012-09-15 08:37','5','00009'
union all
select '2012-09-15 08:37','5','00009'
union all
select '2012-09-16 08:37','5','00009'
union all
select '2012-09-17 08:37','5','00009'
union all
select '2012-09-18 08:37','5','00009'
union all
select '2012-09-18 08:37','5','00009'
union all
select '2012-09-20 08:37','5','00009'
union all
select '2012-09-21 08:37','5','00009'
union all
select '2012-09-22 08:37','5','00009'
union all
select '2012-09-23 08:37','5','00009'
union all
select '2012-09-24 08:37','5','00009';
--sqlserver语句
select t4.attend_id,
t1.plan_id,
t1.cardno,
case when substring(t4.start_time,0,11)=t1.dt then t4.start_time else t1.dt+' 00:00' end start_time,
case when substring(t4.end_time,0,11)=t1.dt then t4.end_time else t1.dt+' 00:00' end end_time
from
(select distinct substring(start_time,0,11) dt,plan_id,cardno from t5)t1
left join t4 on t1.dt=substring(t4.start_time,0,11) and t1.cardno=t4.cardno
order by t1.dt;
--oracle
select t4.attend_id,
t1.plan_id,
t1.cardno,
case when substr(t4.start_time,0,10)=t1.dt then t4.start_time else t1.dt||' 00:00' end start_time,
case when substr(t4.end_time,0,10)=t1.dt then t4.end_time else t1.dt||' 00:00' end end_time
from
(select distinct substr(start_time,0,10) dt,plan_id,cardno from t5)t1
left join t4 on t1.dt=substr(t4.start_time,0,10) and t1.cardno=t4.cardno
order by t1.dt;

结果:
4613 5 00009 2012-09-11 08:37 2012-09-11 00:00
NULL 5 00009 2012-09-12 00:00 2012-09-12 00:00
4614 5 00009 2012-09-13 08:53 2012-09-13 18:01
NULL 5 00009 2012-09-14 00:00 2012-09-14 00:00
4615 5 00009 2012-09-15 08:54 2012-09-15 18:02
NULL 5 00009 2012-09-16 00:00 2012-09-16 00:00
4616 5 00009 2012-09-17 08:30 2012-09-17 18:03
4617 5 00009 2012-09-18 08:37 2012-09-18 19:45
4619 5 00009 2012-09-20 08:37 2012-09-20 18:45
4620 5 00009 2012-09-21 08:37 2012-09-21 00:00
NULL 5 00009 2012-09-22 00:00 2012-09-22 00:00
4621 5 00009 2012-09-23 08:37 2012-09-23 18:35
NULL 5 00009 2012-09-24 00:00 2012-09-24 00:00

有问题再追问吧,望采纳。



你可以先做个临时表,然后插入1-31日的日期空记录(最大日期处决于自然月的最大日期),然后将人员考勤记录依次按对应字段插入至临时表中。这样,对于每一天,有考勤才有数据,没有就为空。
我们是深圳庆丰软件工作室,主打产品为庆丰人事考勤工资管理系统,功能强大,你可以下载试用下。


老边区19647918398: SQL语句 实现时间段内没有打卡记录每一天 -
傅尝乐沙: 你这个得有两个表吧,一个记录表 a,一个人员的信息表 b select * from b where b.id not in (select id from a where a.time between '2012-09-01' and '2012-09-06') order by b.id

老边区19647918398: 如何查出连续三天不打卡的人员清单 -
傅尝乐沙: 试试: select * from employe where iccardid not in (select iccardid from 考勤表 where iotime > 开始时间 and iotime =开始时间 iotime

老边区19647918398: 用SQL语句查询出没有打卡的员工 -
傅尝乐沙: 使用 except减集:用第一个查询的行,减去和第二个查询中一样的行. select ID from EmployeeInfo except select ID from TimeInfo

老边区19647918398: 易通考情机T - 480为什么考情日报表显示不出打卡时间 -
傅尝乐沙: 应该是没有设置班次和人员排班,您查看的是原始数据,不是报表里生成的数据

老边区19647918398: 科密考勤机班次排班都做了,可为何考勤报表里还是没有打卡时间?
傅尝乐沙: 先看下刷卡明细表有没有数据,如果没有,说明考勤机的数据没有采集回来,如果采集数据全是无效,说明考勤机上注册的登记号码和考勤软件中的登记号码不一致.如果刷卡明细表有数据,只是日考勤明细表显示不了刷卡时间,那一般是班次设置(起始刷卡时间、上班时间、下班时间、终止刷卡时间,注意后一个时间比前一个大,如果小,就表示跨夜)或排班问题.你可以把你的班次时间截图出来看下.

老边区19647918398: MY SQL怎么查出员工哪天旷工 -
傅尝乐沙: 用count函数group by的时辰按照礼拜去分组

老边区19647918398: sql查询一段时期里每一天所有员工的打卡记录 -
傅尝乐沙: 1、某一位员工在指定时间段里每一天的最早和最迟打卡记录 select min(card_no) card_no,min(fname) fname,min(rec_dat) start_time,max(rec_dat) end_time from EV_LOG where card_no='员工卡号' and evnt_descrp='Access granted' and ...

老边区19647918398: SQlSever2008数据库,统计某人出勤、缺勤的天数及日期段 -
傅尝乐沙: SQlSever2008数据库,统计某人出勤、缺勤的天数及日期段 如果你这个是最简单的考勤记录,不涉及到三班倒和加班之类的,那么一个最简单的表是这样 员工表(不用说了吧,基本信息) 记录表(日期、员工号、开始时间、结束时间、出勤情况) 如果自动打卡机,获取的就是工号、日期、时间.

老边区19647918398: SQl 查询一个月所有员工的最早打卡时间和最晚打卡时间 -
傅尝乐沙: select Job_number, trunc(worktime-5/24) as Work_day,min(worktime),max(worktime) from table_name group by Job_number, trunc(worktime-5/24) order by 1,2

老边区19647918398: SQL数据库问题,急!!!!!
傅尝乐沙: select name, sum(case when Type='迟到' then 1 else 0 end) 迟到次数, sum(case when Type='早退' then 1 else 0 end) 早退次数, sum(case when Type='缺勤' then 1 else 0 end) 缺勤次数 from kaoqin group by name

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网