0

这是我一直在使用的查询:

select serial_number, DAQ.qtag_no, min(TOH.start_time), order_number, DAQ.creation_time, qtag_status, ar_code, pro_foundin, pro_category, root_cause, remark from UNIT U 
left JOIN WORK_ORDER WO ON WO.order_key = U.order_key
left join TRACKED_OBJECT_HISTORY TOH on TOH.tobj_key = U.unit_key
WHERE U.creation_time > '01/01/2012' AND U.creation_time < '07/20/2012'
AND order_number NOT LIKE '[R]%' 
group by serial_number, qtag_no, order_number, DAQ.creation_time, qtag_status, ar_code, pro_foundin, pro_category, root_cause, remark
order by serial_number

现在我得到 3280 个结果。在此设置中,有不同的站点,例如“组装”、“诊断”等。我的 min(TOH.start_time) 列的目标是返回组装站的第一个开始时间,但目前它正在返回第一个任何车站的开始时间。但是,如果我添加另一个 WHERE 子句来指定站 (TOH.op_name = 'Assembly'),它会限制结果的数量(低至 2700)。我想保留 3280 结果,而不是对于未扫描到装配站的单元,为 min(TOH.start_time) 列返回 NULL。我尝试使用 case 函数,但这需要我在 group by 子句中包含 TOH.op_name,而我并不在寻找它。谢谢!

4

1 回答 1

1

你应该能够将你的条件添加到我认为的左连接中,如下所示:

select  serial_number, 
    DAQ.qtag_no, 
    min(TOH.start_time), 
    order_number, 
    DAQ.creation_time, 
    qtag_status, 
    ar_code, 
    pro_foundin, 
    pro_category, 
    root_cause, 
    remark 
from UNIT U 
left 
JOIN    WORK_ORDER WO 
    ON  WO.order_key = U.order_key
left 
join    TRACKED_OBJECT_HISTORY TOH 
    on  TOH.tobj_key = U.unit_key
    and TOH.op_name = 'Assembly'
WHERE   U.creation_time > '01/01/2012' AND U.creation_time < '07/20/2012'
AND     order_number NOT LIKE '[R]%' 
group by serial_number, qtag_no, order_number, DAQ.creation_time, qtag_status, ar_code, pro_foundin, pro_category, root_cause, remark
order by serial_number
于 2012-07-31T17:07:48.697 回答