如何在 JOIN 中求和,以避免在 SELECT 语句中出现两个 SUM 问题

How to SUM in JOIN, to avoid issue with two SUM in SELECT statement

我已经阅读了很多关于类似问题的帖子,但是 none 找到了解决方案,所以我发布了一个新问题。这两个特别解决了类似的问题:

SQL SELECT with multiple tables and SUM

How to write subquery inside the OUTER JOIN Statement

这是我拥有的代码的第一个版本及其产生的问题。这是在 DB2 for iSeries 数据库中。我刚得到一个通用 ODBC error 1004,这意味着 SQL 没问题,但数据库不喜欢代码的某些方面。我已验证 table 名称和列名是正确的。

SELECT i.IMFGR || i.ICOLOR || i.IPATT Item, i.INAME DESC1, i.INAME2 DESC2, 
       i.IPOL1 PC1, i.IPOL2 PC2, i.IPOL3 PC3, SUM(s.JONHAN) QOH, i.IUNITS UM, 
       SUM(inv.INEXTP) Sales, i.IINVEN INV, i.ICCTR CC, i.ICLAS1 I1, i.IDISCD DDate, 
       i.ILPODT LOrder, i.IAVGC AVGC 
FROM QS36F.ITEM i
JOIN QS36F.ITEMSTK s
     ON i.IMFGR || i.ICOLOR || i.IPATT = s.JMFGR || s.JCOLOR || s.JPAT
        JOIN QS36F.INVOICED inv
     ON inv.INMFGR || inv.INCOLO || inv.INPATT = i.IMFGR || i.ICOLOR || i.IPATT
WHERE (i.IMFGR = '" & man & "') AND ((i.IPOL1 = 'SP') OR (i.IPOL2 = 'SP') OR 
      (i.IPOL3 = 'SP') OR (i.IPOL1 = 'DI') OR (i.IPOL2 = 'DI') OR (i.IPOL3 = 'DI'))
GROUP BY i.IMFGR || i.ICOLOR || i.IPATT, i.INAME, i.INAME2, i.IPOL1, i.IPOL2, i.IPOL3, 
         i.IUNITS, i.IINVEN, i.ICCTR, i.ICLAS1, i.IDISCD, i.ILPODT, i.IAVGC
ORDER BY i.IMFGR || i.ICOLOR || i.IPATT

这不起作用,因为它导致 SUM(s.JONHAN) QOH 被乘以多次,因此不准确。

阅读一些话题后,我似乎需要将第二个 SUM 放在 JOIN 语句中。但是,我仍然收到 ODBC error 1004,所以我不确定我错过了什么。

SELECT i.IMFGR || i.ICOLOR || i.IPATT Item, i.INAME DESC1, i.INAME2 DESC2, 
       i.IPOL1 PC1, i.IPOL2 PC2, i.IPOL3 PC3, SUM(s.JONHAN) QOH, i.IUNITS UM, 
       inv.Sales, i.IINVEN INV, i.ICCTR CC, i.ICLAS1 I1, i.IDISCD DDate,
       i.ILPODT LOrder, i.IAVGC AVGC 
FROM QS36F.ITEM i
JOIN QS36F.ITEMSTK s
     ON i.IMFGR || i.ICOLOR || i.IPATT = s.JMFGR || s.JCOLOR || s.JPAT
LEFT OUTER JOIN (SELECT INMFGR || INCOLO || INPATT Item, SUM(INEXTP) Sales
                 FROM QS36F.INVOICED
                 GROUP BY INMFGR, INCOLO, INPATT) inv
                     ON inv.Item = i.IMFGR || i.ICOLOR || i.IPATT
WHERE (i.IMFGR = '" & man & "') AND ((i.IPOL1 = 'SP') OR (i.IPOL2 = 'SP') OR 
      (i.IPOL3 = 'SP') OR (i.IPOL1 = 'DI') OR (i.IPOL2 = 'DI') OR (i.IPOL3 = 'DI'))
GROUP BY i.IMFGR || i.ICOLOR || i.IPATT, i.INAME, i.INAME2, i.IPOL1, i.IPOL2, i.IPOL3, 
         i.IUNITS, i.IINVEN, i.ICCTR, i.ICLAS1, i.IDISCD, i.ILPODT, i.IAVGC
ORDER BY i.IMFGR || i.ICOLOR || i.IPATT

在添加第二个 SUMJOIN 语句之前一切正常。我可以在第二个 JOIN 中对 SELECT 进行查询,它会正常运行。

首先,这个:

JOIN QS36F.ITEMSTK s
     ON i.IMFGR || i.ICOLOR || i.IPATT = s.JMFGR || s.JCOLOR || s.JPAT

是个坏主意,真的应该

JOIN QS36F.ITEMSTK s
     ON i.IMFGR = s.JMFGR
        and i.ICOLOR = s.JCOLOR 
        and i.IPATT =  s.JPAT

其次,我没有看到任何明显的错误。如果您可以查看为 ODBC 请求提供服务的 QZDASOINIT 作业的作业日志并报告 DB2 抛出的实际错误,将会有所帮助。

话虽如此,我更喜欢通用 Table 表达式 (CTE) 而不是嵌套 Table 表达式 (NTE)

WITH inv as (SELECT INMFGR, INCOLO, INPATT, SUM(INEXTP) Sales
               FROM QS36F.INVOICED
              GROUP BY INMFGR, INCOLO, INPATT
             )
, s as (SELECT JMFGR, JCOLOR, JPAT, sum(JOHHAN) TotalOH
          FROM QS36F.ITEMSTK
         GROUP BY JMFGR, JCOLOR, JPAT
       )
SELECT i.IMFGR || i.ICOLOR || i.IPATT Item, i.INAME DESC1, i.INAME2 DESC2, 
       i.IPOL1 PC1, i.IPOL2 PC2, i.IPOL3 PC3, s.TotalOH QOH, i.IUNITS UM, 
       inv.Sales, i.IINVEN INV, i.ICCTR CC, i.ICLAS1 I1, i.IDISCD DDate,
       i.ILPODT LOrder, i.IAVGC AVGC 
FROM QS36F.ITEM i
JOIN s
      ON i.IMFGR = s.INMFGR
         and i.ICOLOR = s.INCOLOR 
         and i.IPATT =  s.INPATT
LEFT OUTER JOIN inv
       ON i.IMFGR = inv.JMFGR
            and i.ICOLOR = inv.JCOLOR 
            and i.IPATT =  inv.JPAT
WHERE (i.IMFGR = '" & man & "') AND ((i.IPOL1 = 'SP') OR (i.IPOL2 = 'SP') OR 
      (i.IPOL3 = 'SP') OR (i.IPOL1 = 'DI') OR (i.IPOL2 = 'DI') OR (i.IPOL3 = 'DI'))
ORDER BY i.IMFGR, i.ICOLOR, i.IPATT