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

时间:2015-02-19 02:10:17

标签: sql join db2 ibm-midrange

我已经阅读了很多有关类似问题的帖子,但没有一个会导致解决方案,所以我发布了一个新问题。这些是特别针对类似问题的两个问题:

SQL SELECT with multiple tables and SUM

How to write subquery inside the OUTER JOIN Statement

这是我拥有的代码的第一个版本及其创建的问题。这是在DB2 for iSeries数据库中。我只是得到一个通用的ODBC error 1004,这意味着SQL很好,但数据库并不喜欢代码。我已经验证了表名和列名是否正确。

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语句之前,一切都很顺利。我可以对第二个SELECT中的JOIN进行查询,然后按原样运行。

1 个答案:

答案 0 :(得分:3)

首先,这个:

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引发的实际错误,这将有所帮助。

话虽如此,我更倾向于使用公用表表达式(CTE)而不是嵌套表表达式(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