Skip to content
Advertisement

Max from joined table based on value from first table

I have 2 tables.

First holds job details, second one the history of those job runs. First one also contains job period, per customer which is minimum time to wait before running next job for same customer. The time comparison needs to happen on started_on field of second table.

I need to find out the job ids to run next.

Schemas

  1. job_details table
  1. job_run_history table

Sample data for job_details table:

Sample data for job_run_history table:

Expected output (When run at 2021-07-01 14:38:56):

    1. id => 1 did NOT get selected because the last job started within last 1 minute
    1. id => 2 DID get selected because the last job started more than last 1 minute ago
    1. id => 3 DID get selected because it has no run history

I have tried this, but this doesn’t compare with max of start_time, hence, doesn’t work:

MySql Version: 5.7.34

Any help please? Thanks in advance..

Advertisement

Answer

I’m not sure what’s this filter about since you didn’t explain it in your question so I didn’t put it in the query: jrh.status not in ('STREAMING','STREAMED','UPLOADING'). However, I’m sure you can implement it in the query I posted.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement