How to get the last step of a certain job and identify if the status of the last process of every job is equal to 1. If the status of the last process is equal to one then display the value.
JOB TABLE (prepressjobs)
+-----------+--------------+------------+
| ID | SchedNum | Item Name |
+-----------+--------------+------------+
| 1 | JITE7ERUK | Item 1 |
| 2 | JV7FSW26Y | Item 2 |
| 3 | JQFXV4H3X | Item 3 |
+-----------+--------------+------------+
OPERATION TABLE (operation)
+-----------+--------------+------------+-----------+----------+
| ID | SchedNum | Job Name | Step | Status |
+-----------+--------------+------------+-----------+----------+
| 1 | JITE7ERUK | Designing | 0 | 1 |
| 2 | JITE7ERUK | Sample | 1 | 1 |
| 3 | JITE7ERUK | Printing | 2 | 0 |
| 4 | JV7FSW26Y | Designing | 0 | 1 |
| 5 | JV7FSW26Y | Sample | 1 | 0 |
| 6 | JQFXV4H3X | Designing | 0 | 1 |
| 7 | JQFXV4H3X | Sample | 1 | 1 |
+-----------+--------------+------------+-----------+----------+
MY QUERY
SELECT *
FROM operation p1
INNER JOIN prepressjobs
ON prepressjobs.pj_schednum = p1.operation_schednum
WHERE EXISTS(SELECT MAX(operation_step) FROM (SELECT DISTINCT(operation_schednum) FROM operation )p2)
AND p1.operation_status = 0 ;
EXPECTED RESULT
+-----------+--------------+------------+-----------+----------+
| 7 | JQFXV4H3X | Sample | 1 | 1 |
+-----------+--------------+------------+-----------+----------+