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 6567129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:17:58+00:00 2026-05-25T14:17:58+00:00

I have tables containing the following fields among others in a application for a

  • 0

I have tables containing the following fields among others in a application for a transportation company…

jempe_users

+------------------------------+
| Field                        |
+------------------------------+
| user_id                      |
| user_user_group_id           |
| user_type                    | 
| user_real_name               |
| user_drv_code                |
+------------------------------+

jempe_user_types:

+---------------------+
| Field               |
+---------------------+
| user_type_id        |
| user_type_name      |
+---------------------+

employee_clock_events:

+-----------------------+
| Field                 |
+-----------------------+
| ece_id                |
| ece_employee_id       |
| ece_type              |
| ece_datetime          |
| ece_active            |
+-----------------------+

What I need to do is suggest three drivers that are currently working and not on break that can be assigned to pick up a patient.

The jempe_users tables contains employees as well as some clients of the transportation company. The user types table has a record where the user_type_name = driver and the user_type field in the jempe_users table references the jempe_user_types table.

The employee_clock_events table is used to clock employees in and out during their shift. The ece_type field is enum(‘shift_start’, ‘shift_brake’, ‘shift_resume’, ‘shift_stop’). The ece_datetime field stores a timesatmp of when the shift event took place.

So what I need to do is find driver user types in user_group 16, 17, or 18 that are currently working today and who’s most recent shift ece_type is either ‘shift_start’ or ‘shift_resume’.

I’ve tried dozens of variations of the following but can’t seem to select employees WHO ARE CURRENTLY working. I seem to have no problems selected employees that have at least worked at some point today.

Tried variations of this…

SELECT user_id, driver_name, driver_code
FROM
(
    SELECT
        u.user_id AS user_id,
        u.user_real_name AS driver_name,
        u.user_drv_code AS driver_code,
        e.ece_type AS event_type,
        e.ece_datetime AS event_datetime
    FROM employee_clock_events AS e
    INNER JOIN jempe_users AS u 
        ON e.ece_employee_id = u.user_id
    LEFT JOIN jempe_user_types AS ut
        ON u.user_type = ut.user_type_id
    WHERE
        e.ece_shift_date = '2011-09-13'
        AND ut.user_type_name = 'driver'
        AND u.user_user_group_id IN (16, 17, 18)
        AND NOT ISNULL(u.user_drv_code)
        AND u.user_active = 1
) AS TEMPORARY
GROUP BY user_id
HAVING MAX(event_datetime)

And this…

SELECT u.user_id AS user_id, u.user_real_name AS driver_name, u.user_drv_code AS driver_code, MAX(e.ece_datetime) AS current_event_datetime
FROM employee_clock_events AS e
LEFT JOIN jempe_users AS u 
    ON e.ece_employee_id = u.user_id
LEFT JOIN jempe_user_types AS ut
    ON u.user_type = ut.user_type_id
WHERE
    (e.ece_type = 'shift_start' OR e.ece_type = 'shift_resume')
    AND e.ece_shift_date = '2011-09-13'
    AND ut.user_type_name = 'driver'
    AND u.user_user_group_id IN (16, 17, 18)
    AND NOT ISNULL(u.user_drv_code)
    AND u.user_active = 1
GROUP BY e.ece_employee_id

Where am I going wrong? Thanks for the help.

  • 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-05-25T14:17:59+00:00Added an answer on May 25, 2026 at 2:17 pm

    Your problem is determining what the most recent clock event is per employee. That part of the problem can be solved as follows:

     CREATE VIEW MRClockByEmployee (ece_employee_ID, ece_most_recent_datetime) AS
        AS SELECT ece_employee_ID, MAX(ece_datetime) 
        FROM employee_clock_events
        GROUP BY ece_employee_id;
    
     CREATE VIEW MRClockInfoByEmployee 
         (ece_id, ece_employee_ID, ece_type, ece_datetime, ece_active) AS
     SELECT CE.* FROM employee_clock_events CE JOIN MRClockByEmployee MR
        ON CE.ece_employee_ID = MR.ece_employee_id AND
           CE.ece_datetime = MR.ece_most_recent_datetime
    
     SELECT ece_employee_id FROM MRClockInfoByEmployee
        WHERE type IN ('shift_start', 'shift_resume')
    

    You can then JOIN the contents of that second view back to your other tables to get the other information and take the TOP 3 records for your three possible drivers.

    Note that you could do all that in a single, more complex, query but I think the concept of “employee current status” is important and reusable enough that it’s worthwhile encapsulating the logic in a view.

    The final solution (where you JOIN the tables together) will not use a GROUP BY. That is because GROUP BY must be used relatively early in the solution algorithm in order to create a list of employee ids and most recent datetime values; that list is then joined back to the employee table to get the a list of most recent records from that table.

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

Sidebar

Related Questions

I have several tables, containing (a.o.) the following fields: tweets: -------------------------- tweet_id ticker created_at
I have a data frame containing the following fields: a, b, c. a and
I have a table with a Name field containing data similar to the following:
I have a database containing three tables: practices - 8 fields patients - 47
I have a MySQL database containing the following tables: Table: Professor Attributes: ID, ProfessorName
Let's say I have a table containing following data: | id | t0 |
Suppose I have an Oracle 11.2 database containing the following table: TABLE: SURVEY PARAMETER
I have a number of tables containing some basic (business related) mapping data. What's
I have a small database with tables containing a small amount of data which
I have two tables, one containing cities and one containing countries. Those are bi-directional

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.