2013-08-21 103 views
1

在我的代碼,我需要得到關於某些部件編號的數據,我遇到了一個奇怪的問題,當我運行代碼:能增加更多的左外連接影響以前的列

SELECT 
       tblensembleunepiece.ENSPIECEID, 
       tblensembleunepiece.NOPIECE As 'Part Number', 
       tblensembleunepiece.DESCRIPTIONENSP As 'Description', 
       tblensembleunepiece.QTEMIN As 'Min', 
       tblensembleunepiece.QTEMAX As 'Max', 
       tbltypepiece.TITRETYPE, 
       case 
           when CONSOMMABLE='true' Then 'Consumeable' 
           else 'Rotable' end As 'Utilization', 
       sum(tblitem.NBPOURPACK) As 'Serviceable', 
       sum(case when tblquarantaine.BER = 'false' Then 1 else 0 END) As 'Quarantine Repairable', 
       sum(case when tblquarantaine.BER = 'true' Then 1 else 0 end) As 'Quarantine BER/Scrap', 
       sum(case when tblbonsortieitem.VAREVENIR = 'true' Then 1 else 0 end) As 'Repair Line', 

    sum(case when (tblhistorique.REMARQUE LIKE "Added to operation cost%" OR tblhistorique.REMARQUE LIKE "Added to operational cost%") 
     then (substring_index(LTRIM(substring_index(tblhistorique.REMARQUE, 'Qty:', -1)), '.', 1)+0) 
     when (tblhistorique.REMARQUE LIKE "Removed from operation cost%" OR tblhistorique.REMARQUE LIKE "Removed from operational cost%") 
     then (substring_index(LTRIM(substring_index(tblhistorique.REMARQUE, 'Qty:', -1)), ' ', 1)*(-1)) else 0 end) 
FROM 
       tblensembleunepiece 
       JOIN tbltypepiece ON tblensembleunepiece.TYPEPIECEID = tbltypepiece.TYPEPIECEID 
       JOIN tblitem ON tblensembleunepiece.ENSPIECEID = tblitem.ENSPIECEID 
       LEFT OUTER JOIN tblquarantaine ON tblquarantaine.ITEMID = tblitem.ITEMID 
       LEFT OUTER JOIN tblbonsortieitem ON tblbonsortieitem.ITEMID = tblitem.ITEMID 
       LEFT OUTER JOIN tblhistorique ON tblhistorique.ITEMID = tblitem.ITEMID 
WHERE 
       tblensembleunepiece.NOPIECE<>'' 

GROUP BY tblensembleunepiece.ENSPIECEID; 

然後我得到的錯誤數據。雖然,在SELECT子句中添加最後一個sum語句並在FROM子句中添加最後一個LEFT OUTER JOIN之前,一切正常。

Serviceable列是在添加額外代碼時發生錯誤的列,它輸出的值比它們應該大4倍(不是所有值,但大多數)。

添加更多左外連接可能會導致以前的列更改嗎?

+0

爲什麼要添加一個不影響所選內容的連接?這將是有點毫無意義... –

+0

我不能使用子查詢數據庫不支持它 – JahKnows

回答

3

是的。這只是說新表中有多個匹配的表。

解決此問題的一種方法是在執行連接之前,通過用於連接的鍵來聚合新表。

換句話說,表tblhistorique有多行與ITEMID相同。這會乘以聚合處理的行數,導致對某些值進行多次計數。

編輯:

你聚集僅使用列從tblhistorique。您可以將邏輯移動到子查詢中並進行聚合:

SELECT . . ., 
     val 
FROM 
       tblensembleunepiece 
       JOIN tbltypepiece ON tblensembleunepiece.TYPEPIECEID = tbltypepiece.TYPEPIECEID 
       JOIN tblitem ON tblensembleunepiece.ENSPIECEID = tblitem.ENSPIECEID 
       LEFT OUTER JOIN tblquarantaine ON tblquarantaine.ITEMID = tblitem.ITEMID 
       LEFT OUTER JOIN tblbonsortieitem ON tblbonsortieitem.ITEMID = tblitem.ITEMID 
       LEFT OUTER JOIN (select itemId, 
             sum(case when (th.REMARQUE LIKE "Added to operation cost%" OR th.REMARQUE LIKE "Added to operational cost%") 
     then (substring_index(LTRIM(substring_index(th.REMARQUE, 'Qty:', -1)), '.', 1)+0) 
     when (th.REMARQUE LIKE "Removed from operation cost%" OR tblhistorique.REMARQUE LIKE "Removed from operational cost%") 
     then (substring_index(LTRIM(substring_index(th.REMARQUE, 'Qty:', -1)), ' ', 1)*(-1)) else 0 end) as val 
           from tblhistorique th 
           ) th ON th.ITEMID = tblitem.ITEMID 
. . . 
+0

你是正確的,每個ITEMID在tblhistorique中有多個實例。這是否意味着查詢在新列中找到的ITEMID的每個新實例也都正在更新以前的列?他們爲什麼要這樣做? 我如何彙總新表,這是什麼意思? – JahKnows

+0

有沒有辦法來凍結我已經有的時候去下一個列? – JahKnows

+0

如果在tblhistorique(全表掃描+ JOIN無w/o索引)中有很多不同的ITEMID,這可能會導致性能較差。我認爲最好將除了有問題的JOIN之外的所有內容移動到內部查詢中,然後再從中選擇*並執行最後的JOIN。 – Vatev