2 ldp2816 ldp2816 于 2014.12.02 00:18 提问

SQL 数据库考勤为打卡记录查询

现在有一张表A,存放员工的打开记录,一张表B,记录每个月除了双休日的具体日期,现在想用两个表对比,查询出未打卡的员工名单,请大神帮忙解决下!
表A
1001 素娟 人事部 2014/10/9 08:43 18:16
1001 素娟 人事部 2014/10/10 08:56 18:16
1001 素娟 人事部 2014/10/14 08:55 18:18
1001 素娟 人事部 2014/10/15 08:50 18:16
1001 素娟 人事部 2014/10/16 08:49 18:16
1002 娟 人事部 2014/10/9 08:43 18:16
1002 娟 人事部 2014/10/10 08:56 18:16
1002 娟 人事部 2014/10/15 08:50 18:16
1002 娟 人事部 2014/10/16 08:49 18:16
...
表B
2014-10-06 16:37:54.007
2014-10-07 16:37:54.007
2014-10-08 16:37:54.007
2014-10-09 16:37:54.007

6个回答

fredrickhu
fredrickhu   Ds   Rxr 2014.12.03 15:37
已采纳

select
distinct a.name
from
(
select * from (select distinct name from a) as a cross join (select distinct times from a) as b
) as a
where not exists(select 1 from b where datediff(dd,times,a.times)=0)

ldp2816
ldp2816   2014.12.02 09:23

有没有大神能帮忙看下,谢谢啦

lk13962517093
lk13962517093   2014.12.02 13:34

表B干什么用的
另外无法知道所有员工。

ldp2816
ldp2816 表B是每个月的除去双休日的具体日期,所有员工在考勤表里都有,可以groupby出来
大约 3 年之前 回复
firstfinal
firstfinal   2014.12.02 15:21

你们是不是还应该有一个员工表啊?不然怎么知道所有的员工。

fredrickhu
fredrickhu   Ds   Rxr 2014.12.03 15:26

select
a.name
from
(
select * from (select dintinct name from a) as a cross join (select distinct times from a) as b
) as a
where not exists(select 1 from b where times=a.times)

fredrickhu
fredrickhu   Ds   Rxr 2014.12.03 15:34

--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table A,[dep] varchar(6),[times] datetime,[C5] datetime)
insert [A]
select 1001,'素娟','人事部','2014/10/9 08:43','18:16' union all
select 1001,'素娟','人事部','2014/10/10 08:56','18:16' union all
select 1001,'素娟','人事部','2014/10/14 08:55','18:18' union all
select 1001,'素娟','人事部','2014/10/15 08:50','18:16' union all
select 1001,'素娟','人事部','2014/10/16 08:49','18:16' union all
select 1002,'娟','人事部','2014/10/9 08:43','18:16' union all
select 1002,'娟','人事部','2014/10/10 08:56','18:16' union all
select 1002,'娟','人事部','2014/10/15 08:50','18:16' union all
select 1002,'娟','人事部','2014/10/16 08:49','18:16'
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table B
insert [B]
select '2014-10-06 16:37:54.007' union all
select '2014-10-07 16:37:54.007' union all
select '2014-10-08 16:37:54.007' union all
select '2014-10-09 16:37:54.007'
--------------开始查询--------------------------
select
distinct a.name
from
(
select * from (select distinct name from a) as a cross join (select distinct times from a) as b
) as a
where not exists(select 1 from b where times=a.times)
----------------结果----------------------------

/* name


素娟

(2 行受影响)
*/

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!