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 倍(不是所有值,而是大多数值)。

添加更多的左外连接是否有可能导致前面的列发生变化?

4

1 回答 1

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 
. . .
于 2013-08-21T14:12:01.680 回答