SELECT *
FROM (
(SELECT A.WORK_ORDER
,A.PALLET_NO
,A.SERIAL_NUMBER
,B.PROCESS_NAME
,'10'AS STEP
,'QC CODE'AS ACTION
,
case
when A.OUT_PROCESS_TIME=C.REC_TIME or A.PROCESS_ID=F.RP_PROCESS_ID
then E.DEFECT_CODE
else 'PASS'
end as "DEFECT_CODE"
,
case
when A.OUT_PROCESS_TIME=C.REC_TIME
then E.DEFECT_DESC
else null
end as "不良描述1"
,
A.OUT_PROCESS_TIME "OutProcessTime"
,
case
when A.OUT_PROCESS_TIME=C.REC_TIME
then F.REPAIR_TIME
else null
end as "(Repair Time)"
,
Case
when A.OUT_PROCESS_TIME=C.REC_TIME or A.PROCESS_ID=F.RP_PROCESS_ID
then G.REASON_CODE
else null
end as "原因代碼(Reason Code)"
,
case
when A.OUT_PROCESS_TIME=C.REC_TIME or A.PROCESS_ID=F.RP_PROCESS_ID
then G.REASON_DESC
else null
end as "原因描述(Reason Desc)"
,
case
when A.OUT_PROCESS_TIME=C.REC_TIME or A.PROCESS_ID=F.RP_PROCESS_ID
then F.LOCATION
else null
end as "位置(Location)"
,
case
when A.OUT_PROCESS_TIME=C.REC_TIME or A.PROCESS_ID=F.RP_PROCESS_ID
then H.DUTY_DESC
else null
end as "責任描述(DutyDesc)"
FROM SAJET.G_SN_TRAVEL A
,SAJET.SYS_PROCESS B
,SAJET.G_SN_DEFECT C
, SAJET.SYS_DEFECT E
, SAJET.G_SN_REPAIR F
, SAJET.SYS_REASON G
, SAJET.SYS_DUTY H
WHERE A.PROCESS_ID = B.PROCESS_ID
AND A.SERIAL_NUMBER=C.SERIAL_NUMBER
AND E.DEFECT_ID=C.DEFECT_ID
AND F.RECID=C.RECID
AND F.REASON_ID=G.REASON_ID
AND F.DUTY_ID=H.DUTY_ID
AND B.PROCESS_ID NOT IN '100056'
AND B.PROCESS_NAME not in 'MAC MAP'
AND B.PROCESS_NAME NOT LIKE ('%REPAIR%')
AND A.SERIAL_NUMBER ='IXJ147241' )
UNION ALL
(SELECT A.WORK_ORDER
,A.PALLET_NO
,A.SERIAL_NUMBER
,B.PROCESS_NAME
,'10'AS STEP
,'Install component - S/N' AS ACTION
,C.BARCODE AS DEFECT_CODE
,null
,A.OUT_PROCESS_TIME
,null
,null
,null
,null
,null
FROM SAJET.G_SN_TRAVEL A,SAJET.SYS_PROCESS B , SAJET.G_BARCODE C
WHERE A.PROCESS_ID = B.PROCESS_ID
AND A.SERIAL_NUMBER = C.SERIAL_NUMBER
AND B.PROCESS_NAME ='MAC MAP'
AND A.SERIAL_NUMBER ='IXJ147241'))
!!![https://ithelp.ithome.com.tw/upload/images/20190320/20116136Bl3Rdbhnhw.png](https://ithelp.ithome.com.tw/upload/images/20190320/20116136Bl3Rdbhnhw.png)
union 與 union all 的結果
union 重複資料只顯示一筆.
union all 可以有重複資料
聽我一句勸
關鍵不是「DISTINCT」
你還是先弄清楚主管真正要的是什麼結果
以免白忙一場...
那個 SQL 指令寫那麼複雜/長
好不容易找出了這麼多筆資料
結果你只要留下一筆
就算只要留下一筆
就你貼上來的資料來猜
有幾個可能:
1.OutProcessTime 最新的那一筆(eg.最新/後狀態)
2.DEFECT_CODE 不是 PASS 那一筆(eg.測試未合格項目)
怎麼樣都比「DISTINCT 序號」
要來得有意義些