Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You would left outer join the table to itself:</p> <pre><code>select a.punchId from timePunches a left outer join timePunches b on a.punchId = b.punchDTC_LINK where b.punchDTC_LINK is null and a.punchDirection = 'IN'; </code></pre> <p>This will return the stuff you are looking for, I think.</p> <p>I edited to add the check for punchDirection of in.</p> <p>You can also change the select piece to select out a.*, which would get you everything from the record.</p> <p>Additional Edit:</p> <p>The code above appears to have solved the original problem but what the real problem is when someone checks in twice in a row, then checks out. In MYSQL, this is an incredibly difficult problem, but in Postgres &amp; Oracle, it is relatively simple, and can be solved with windowing functions. I know that this does not solve your problem, but you can probably work out the problem with a script after the fact. In postgres/oracle, the following would find the records that you are looking for:</p> <pre><code>select * from (select a.punchId, lag(b.punchDTC_LINK,1) over ( partition by a.employeeId order by a.punchDATETIME ) prev_link, a.punchDTC_LINK cur_link from timePunches a left outer join timePunches b on a.punchId = b.punchDTC_LINK) where prev_link is null and cur_link is not null; </code></pre> <p>Or something similar. I haven't tested it.</p> <p>As for a script, you could use the following query:</p> <pre><code>select a.punchId, a.employeeID, a.punchDATETIME, a.punchDIRECTION, b.punchDATETIME, b.punchDIRECTION from timePunches a left outer join timePunches b on a.punchId = b.punchDTC_LINK order by a.employeeID, a.punchDATETIME desc; </code></pre> <p>and by looping over the data, and/or writing a script to loop over the data and find places where the b.punchDIRECTION is null and it is not the first record for a given employee, you will easily be able to find the bad records.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload