2017-08-07 43 views
0

如何獲取某個作業的最後一步並確定每個作業的最後一個進程的狀態是否等於1.如果最後一個進程的狀態等於然後顯示值。獲取每個作業的最後進程並識別狀態

作業表(prepressjobs)

+-----------+--------------+------------+ 
| ID  | SchedNum  | Item Name | 
+-----------+--------------+------------+ 
| 1   | JITE7ERUK | Item 1  | 
| 2   | JV7FSW26Y | Item 2  | 
| 3   | JQFXV4H3X | Item 3  | 
+-----------+--------------+------------+ 

動作表(操作)

+-----------+--------------+------------+-----------+----------+ 
| 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 ; 

預期的結果

+-----------+--------------+------------+-----------+----------+ 
| 7   | JQFXV4H3X | Sample  | 1   | 1  | 
+-----------+--------------+------------+-----------+----------+ 
+0

它是MySQL或MS SQL? – Aparna

+0

@Aparna,mySQL。 – Ailyn

+0

@Aparna。它最有可能得到像使用此代碼的最後一行SELECT m1。* FROM操作m1 LEFT JOIN操作m2 ON(m1.operation_schednum = m2.operation_schednum AND m1.operation_id Ailyn

回答

0

最後我得到了它在我自己的作品。但還是要感謝@Aparna的回覆。另外我有一個想法Retrieving the last record in each groupJOIN three tables

QUERY:

SELECT * FROM operation t1 
INNER JOIN prepressjobs 
ON prepressjobs.pj_schednum = t1.operation_schednum 
WHERE EXISTS(SELECT * FROM operation p2 WHERE p2.operation_step = IF((t1.operation_step - 1)=-1,0,(t1.operation_step - 1)) AND p2.operation_status = 1) 
AND t1.operation_processname ='SINGLE DIELINE' 
AND t1.operation_status = 0 
AND prepressjobs.pj_status ='APPROVED' 
0

嘗試below.This在SQL Server

select * from 
(
select *,row_number() over(partition by schednum order by step desc) as rn 
from operations 
) a 
where a.rn=1 and a.status=1;