SQL query to retrieve discrepancies in punch order -


consider table below.

the rule - employee cannot take break (needs clock out) job num 1 before clocking in job num 2. in case employee "a" supposed clock out instead of break on jobnum 1 because later clocked in jobnum#2

is possible write query find in plain sql?

table

idea check if next record proper one. find next record 1 has find first punchtime after current same employee. once information retrieved 1 can isolate record , check fields of interest, jobnum same , [optionally] punch_type 'in'. if not, not exists evaluates true , record output.

select * @punch p -- isolate breaks p.punch_type = 'break' -- ones having no proper entry , not exists (     select null     -- same table     @punch     a.emplid = p.emplid       , a.jobnum = p.jobnum     -- next record has punchtime subquery       , a.punchtime = (select min (n.punchtime)                             @punch n                            n.emplid = p.emplid                              , n.punchtime > p.punchtime                         )     -- optionally might force next record 'in'       , a.punch_type = 'in' ) 

replace @punch table name. -- comment in sql server; if not using database, remove lines. idea tag database , version there faster/better ways this.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -