使用一个数据库,其中每个订单都有一个分配给它的项目,每个项目都必须有一个开始操作,然后才能发生工作操作,一旦完成,就会执行完成操作。
同一个项目可以有多个开始和结束,但工作只能在开始和结束动作之间进行。
我编写了一个脚本来尝试查找在完成后执行工作但没有开始但它没有返回我想要的值的任何情况。
SELECT
O.ORDER_NUMBER
--,*
FROM
[CUSTOMER] [C]
JOIN
[SITE] [S] ON C.CUSTOMER_ID=S.CUSTOMER_ID
JOIN
[ORDER] [O] ON S.SITE_ID=O.SITE_ID
JOIN
[ITEM] [I] ON O.ORDER_ID=I.ORDER_ID
JOIN
(SELECT
MAX(ACTION_ID) AS [START_ID]
,A2.ITEM_ID
FROM
[ACTION] [A2]
JOIN
[ALLOWED_ACTION] [AA2] ON A2.ALLOWED_ACTION_ID = AA2.ALLOWED_ACTION_ID
WHERE
AA2.DESCRIPTION='START'
GROUP BY
A2.ITEM_ID) AS [D] ON I.ITEM_ID=D.ITEM_ID
JOIN
(SELECT
MAX(ACTION_ID) AS [FINISH_ID]
,A3.ITEM_ID
FROM
[ACTION] [A3]
JOIN
[ALLOWED_ACTION] [AA3] ON A3.ALLOWED_ACTION_ID = AA3.ALLOWED_ACTION_ID
WHERE
AA3.DESCRIPTION='FINISH'
GROUP BY
A3.ITEM_ID) AS [F] ON I.ITEM_ID=F.ITEM_ID
JOIN
(SELECT
MAX(ACTION_ID) AS [LAST_ID]
,A4.ITEM_ID
FROM
[ACTION] [A4]
GROUP BY
A4.ITEM_ID) AS [L] ON I.ITEM_ID=L.ITEM_ID
WHERE
(L.LAST_ID>F.FINISH_ID)
AND
(F.FINISH_ID>D.START_ID)
我已经查看了如何从需要在单个查询中首先在 sql server 中添加的表中获得第二高?和其他人喜欢尝试找出答案,但我看不出我做错了什么。
一个典型的结果会给我一个订单号,当我检查动作时,最后一个动作总是一个完成或它有一个开始。
任何帮助对我来说都意义重大。