I am using MySql.
I have table job
that has a primary key job_pk_id
and the table stores details of every job. Also I have a table job_running_status
where job
table's job_pk_id
is a foreign key and this table basically contains records of when a job ran for each job_pk_id
.There will be multiple entries for the same job_pk_id
as the same job runs multiple times. job_running_status
table also has a field job_start_time
that gives the start time for each instance of the running of the job.
Now my requirement is to get the latest job_running_status
for every job
. The latest job_running_status
would be chosen based on the latest job_start_time
(for that particular job only) value in job_running_status
.
I know this can be achieved using INNER JOIN
and ORDER BY job_start_time desc
between job
table and job_running_status
table but my challenge is this ORDER BY
becomes applicable across all the jobs in the JOIN
but I need to be applicable only across records that are corresponding to a particular job.
EDIT
I understand it might be confusing to understand me by just reading so I am providing some examples:
job
table:
job_running_status
table:
My final requirement after joining both the tables
Note: while joining I a should be getting only 1 record corresponding to every JOB table record. This record is chosen based on the latest job_start_time for that JOB.