考虑下表。
规则是-员工不能在进入工作编号2之前从工作编号1休息(需要打卡离开)。在这种情况下,员工A应该在工作编号1上打卡而不是休息,因为他后来打卡到了JobNum#2
有没有可能在纯SQL中编写一个查询来查找它?

发布于 2012-06-06 20:14:17
我的想法是检查下一条记录是否是正确的。要查找下一条记录,必须为同一员工找到当前记录之后的第一个punchtime。一旦检索到此信息,就可以隔离记录本身并检查感兴趣的字段,特别是jobnum是相同的,并且可选地是punch_type 'IN‘。如果不是,则not exists计算结果为true,并输出record。
select *
from @punch p
-- Isolate breaks only
where p.punch_type = 'BREAK'
-- The ones having no proper entry
and not exists
(
select null
-- The same table
from @punch a
where a.emplid = p.emplid
and a.jobnum = p.jobnum
-- Next record has punchtime from subquery
and a.punchtime = (select min (n.punchtime)
from @punch n
where n.emplid = p.emplid
and n.punchtime > p.punchtime
)
-- Optionally you might force next record to be 'IN'
and a.punch_type = 'IN'
)将@ name替换为您的表名。--是Sql Server中的注释;如果您不使用此数据库,请删除此行。标记你的数据库和版本是一个好主意,因为可能有更快/更好的方法来做到这一点。
发布于 2012-06-06 10:42:35
下面是SQL语句
select * from employees e1 cross join employees e2 where e1.JOBNUM = (e2.JOBNUM + 1)
and e1.PUNCH_TYPE = 'BREAK' and e2.PUNCH_TYPE = 'IN'
and e1.PUNCHTIME < e2.PUNCHTIME
and e1.EMPLID = e2.EMPLIDhttps://stackoverflow.com/questions/10907144
复制相似问题