可以添加更多LEFT OUTER JOINS会影响以前的列

时间:2013-08-21 14:08:17

标签: mysql sql sum left-join

在我的代码中,我需要获取有关某些部件号的数据,当我运行代码时遇到了一个奇怪的问题:

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之前,一切正常。

可服务列是在添加额外代码时产生错误的列,它输出的值比它们应该大4倍(不是所有值,而是大多数)。

是否可能添加更多LEFT OUTER JOINS会导致之前的列发生变化?

1 个答案:

答案 0 :(得分: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 
. . .