34,593
社区成员
发帖
与我相关
我的任务
分享
--昨天上了班,今天没有上班
select * from tb t
where exists(select 1 from tb where 工号=t.工号 and 日期>=convert(varchar(10),getdate()-1,120) and 日期<convert(varchar(10),getdate(),120))
and not exists(select 1 from tb where 工号=t.工号 and 日期>=convert(varchar(10),getdate(),120) and 日期<convert(varchar(10),getdate()+1,120))
--昨天没有上班,今天上了班
select * from tb t
where not exists(select 1 from tb where 工号=t.工号 and 日期>=convert(varchar(10),getdate()-1,120) and 日期<convert(varchar(10),getdate(),120))
and exists(select 1 from tb where 工号=t.工号 and 日期>=convert(varchar(10),getdate(),120) and 日期<convert(varchar(10),getdate()+1,120))
--昨天没有上班,今天上了班
select 工号
from [table] a
where 日期 = '昨天'
and exists (
select 1 from [table]
where 日期 = '今天'
and 工号 = a.工号
)
--或者
select 工号
from [table] a
where 日期 = '昨天'
and 工号 in(
select 工号 from [table]
)
select 工号
from [table] a
where 日期 = '昨天'
and not exists (
select 1 from [table]
where 日期 = '今天'
and 工号 = a.工号
)
or:
select a.工号
from [table] a left join [table] b
on b.工号 = a.工号 and b.日期 = '今天'
where a.日期 = '昨天'
and b.日期 is null
--1, 我想知道昨天上了班,今天没有上班
select 工号
from (
select *
from tb
where 日期=dateadd(day,-1,getdate())) a
left join
(select *
from tb
where 日期=getdate()) b
on a.工号=b.
where b.工号 is null