这是我当前的查询:
select serial_number, order_number,
(select TOP 1 PRODUCT_ID from PS_ORD_LINE PL
where PL.ORDER_NO = WO.order_number
and CAT_DESCR = 'SYSTEM' and ORD_LINE_STATUS = 'O'
ORDER BY ORDER_INT_LINE_NO) as model,
(select datediff(minute, min(complete_time), min(start_time))
from TRACKED_OBJECT_HISTORY TOH1
where TOH1.op_name IN ('Assembly', 'Pre-Final')
and TOH1.tobj_key = TOH.tobj_key) as waiting_time1,
from UNIT U
left join WORK_ORDER WO on U.order_key = WO.order_key
left join TRACKED_OBJECT_HISTORY TOH on TOH.tobj_key = U.unit_key
where WO.creation_time > '5/1/12' and WO.creation_time < '7/31/12'
group by serial_number, order_number, tobj_key
中间的部分DateDiff
是我的问题。
所以Assembly
和Pre-Final
是扫描单元的两个不同站点的名称。Assembly
通常排在第一位,紧随其后的是Pre-Final
。
我要做的是计算一个单元扫描出之后Assembly
和该单元扫描到Pre-Final
. complete_time
当一个单元被扫描出站时标记,start_time
当它被扫描到时。
现在,我的查询不起作用,因为两者都start_time
引用complete_time
,Assembly
因为它是第一站。但是,我想complete_time
参考,Assembly
而我想start_time
参考Pre-Final
.
我该怎么做呢?