有张上班记录表table(日期,工号,姓名,上班,下班);我想知道昨天上了班,今天没有上班;或者是昨天没有上班,今天上了班的记录怎么写SQL语句??

解决方案 »

  1.   

    --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
      

  2.   

    昨天上了班,今天没有上班
    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昨天没有上班,今天上了班
    类似,不重复了
      

  3.   

    --昨天没有上班,今天上了班
    select 工号
    from [table] a
    where 日期 = '昨天'
    and  exists (
       select 1 from [table]
       where 日期 = '今天'
       and 工号 = a.工号
       )
    --或者
    select 工号
    from [table] a
    where 日期 = '昨天'
    and  工号 in(
       select 工号 from [table]
          )
      

  4.   

    --昨天上了班,今天没有上班
    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))