Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8435551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:56:45+00:00 2026-06-10T06:56:45+00:00

I’m working on a simple time tracking app. I’ve created a table that logs

  • 0

I’m working on a simple time tracking app.

I’ve created a table that logs the IN and OUT times of employees.

Here is an example of how my data currently looks:

E_ID | In_Out |      Date_Time
------------------------------------
  3  |   I    | 2012-08-19 15:41:52
  3  |   O    | 2012-08-19 17:30:22
  1  |   I    | 2012-08-19 18:51:11
  3  |   I    | 2012-08-19 18:55:52
  1  |   O    | 2012-08-19 20:41:52
  3  |   O    | 2012-08-19 21:50:30

Im trying to create a query that will pair the IN and OUT times of an employee into one row like this:

E_ID |       In_Time       |      Out_Time
------------------------------------------------
  3  | 2012-08-19 15:41:52 | 2012-08-19 17:30:22
  3  | 2012-08-19 18:55:52 | 2012-08-19 21:50:30
  1  | 2012-08-19 18:51:11 | 2012-08-19 20:41:52

I hope I’m being clear in what I’m trying to achieve here.
Basically I want to generate a report that had both the in and out time merged into one row.

Any help with this would be greatly appreciated.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:56:46+00:00Added an answer on June 10, 2026 at 6:56 am

    There are three basic approaches I can think of.

    One approach makes use of MySQL user variables, one approach uses a theta JOIN, another uses a subquery in the SELECT list.

    theta-JOIN

    One approach is to use a theta-JOIN. This approach is a generic SQL approach (no MySQL specific syntax), which can work with multiple RDBMS.

    N.B. With a large number of rows, this approach can create a significantly large intermediate result set, which can lead to problematic performance.

    SELECT o.e_id, MAX(i.date_time) AS in_time, o.date_time AS out_time    
      FROM e `o`
      LEFT
      JOIN e `i` ON i.e_id = o.e_id AND i.date_time < o.date_time AND i.in_out = 'I'
     WHERE o.in_out = 'O'
     GROUP BY o.e_id, o.date_time
     ORDER BY o.date_time
    

    What this does is match every ‘O’ row for an employee with every ‘I’ row that is earlier, and then we use the MAX aggregate to pick out the ‘I’ record with the closest date time.

    This works for perfectly paired data; could produce odd results for imperfect pairs… (two consecutive ‘O’ records with no intermediate ‘I’ row, will both get matched to the same ‘I’ row, etc.)


    correlated subquery in SELECT list

    Another approach is to use a correlated subquery in the SELECT list. This can have sub-optimal performance, but is sometimes workable (and is occasionally the fastest way to return the specified result set… this approach works best when we have a limited number of rows returned in the outer query.)

     SELECT o.e_id
          , (SELECT MAX(i.date_time)
               FROM e `i`
              WHERE i.in_out = 'I'
                AND i.e_id = o.e_id
                AND i.date_time < o.date_time
            ) AS in_time
          , o.date_time AS out_time
       FROM e `o`
      WHERE o.in_out = 'O'
      ORDER BY o.date_time
    

    User variables

    Another approach is to make use of MySQL user variables. (This is a MySQL-specific approach, and is a workaround to the “missing” analytic functions.)

    What this query does is order all of the rows by e_id, then by date_time, so we can process them in order. Whenever we encounter an ‘O’ (out) row, we use the value of date_time from the immediately preceding ‘I’ row as the ‘in_time’)

    N.B.: This usage of MySQL user variables is dependent on MySQL performing operations in a specific order, a predictable plan. The use of the inline views (or “derived tables”, in MySQL parlance) gets us a predictable execution plan. But this behavior is subject to change in future releases of MySQL.

    SELECT c.e_id
         , CAST(c.in_time AS DATETIME) AS in_time
         , c.out_time
      FROM (
             SELECT IF(@prev_e_id = d.e_id,@in_time,@in_time:=NULL) AS reset_in_time
                  , @in_time := IF(d.in_out = 'I',d.date_time,@in_time) AS in_time
                  , IF(d.in_out = 'O',d.date_time,NULL) AS out_time
                  , @prev_e_id := d.e_id  AS e_id
               FROM (
                      SELECT e_id, date_time, in_out 
                        FROM e
                        JOIN (SELECT @prev_e_id := NULL, @in_time := NULL) f
                       ORDER BY e_id, date_time, in_out 
                     ) d
           ) c
     WHERE c.out_time IS NOT NULL
     ORDER BY c.out_time
    

    This works for the set of data you have, it needs more thorough testing and tweaking to ensure you get the result set you want with quirky data, when the rows are not perfectly paired (e.g. two ‘O’ rows with no ‘I’ row between them, an ‘I’ row with no subsequent ‘O’ row, etc.)

    SQL Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.