0

我有 2 个进度数据库查询,我正在尝试将它们合并到一个语句中,但我遇到了错误。这些查询中的每一个都只返回一个数字,我想将这两个数字相加。要么从头开始进行另一个查询。他们都接受一组“DM1”的价值代码,他们都接受1个“产品”。

查询 1

SELECT SUM(opn3.samt)
    FROM PUB.ord ord3, PUB.opn opn3
    WHERE
    ord3.subsnum = opn3.subsnum
    AND ord3.onum = opn3.onum
    AND ord3.DM1 != ''
    AND ord3.DM1 IN('XCWAJC25','WCWAMO73') 
    AND ord3.prdcde = 'CSC' 
    AND ord3.stat != 16
    AND opn3.samt >= 0
    GROUP BY ord3.DM1, ord3.prdcde

查询 2

SELECT SUM((-1 * opn2.samt) + ord2.samt)
    FROM PUB.ord ord2, PUB.opn opn2
    WHERE
    ord2.subsnum = opn2.subsnum
    AND ord2.onum = opn2.onum
    AND ord2.DM1 != ''
    AND ord2.DM1 IN('XCWAJC25','WCWAMO73') 
    AND ord2.prdcde = 'CSC' 
    AND ord2.stat = 16
    AND opn2.samt < 0
    GROUP BY ord2.DM1, ord2.prdcde

合并尝试到目前为止...

SELECT SUM(opn3.samt + (SELECT SUM((-1 * opn2.samt) + ord2.samt)
    FROM PUB.ord ord2, PUB.opn opn2
    WHERE
    ord2.subsnum = opn2.subsnum
    AND ord2.onum = opn2.onum
    AND ord2.DM1 != ''
    AND ord2.DM1 = ord3.DM1
    AND ord2.prdcde = ord3.prdcde 
    AND ord2.stat = 16
    AND opn2.samt < 0
    GROUP BY ord2.DM1, ord2.prdcde
)) as foo

    FROM PUB.ord ord3, PUB.opn opn3
    WHERE
    ord3.subsnum = opn3.subsnum
    AND ord3.onum = opn3.onum
    AND ord3.DM1 != ''
    AND ord3.DM1 IN('XCWAJC25','WCWAMO73') 
    AND ord3.prdcde = 'CSC' 
    AND ord3.stat != 16
    AND opn3.samt >= 0
    GROUP BY ord3.DM1, ord3.prdcde

谢谢

4

2 回答 2

1

我认为这会起作用,尽管有样本数据来验证会很好:

SELECT COALESCE(SUM(a.samt), 0) - COALESCE(SUM(b.samt), 0)
       + COALESCE(SUM(CASE WHEN ord.stat = 16
                                AND b.samt < 0
                           THEN ord.samt END), 0)
FROM PUB.ord ord
LEFT JOIN PUB.opn a
       ON a.subsnum = ord.subsnum
          AND a.onum = ord.onum
          AND a.samt >= 0
          AND ord.stat != 16
LEFT JOIN PUB.opn b
          ON b.subsnum = ord.subsnum
             AND b.onum = ord.onum
             AND b.samt < 0
             AND ord.stat = 16
WHERE ord.DM1 IN('XCWAJC25', 'WCWAMO73') 
      AND ord.prdcde = 'CSC'
GROUP BY ord.DM1

关于查询/东西的注意事项:

  • 始终明确限定连接,不要使用逗号分隔的FROM子句
  • 我认为您不需要ord.DM1 != '',因为值必须在特定的集合中
  • Putting a clause into a LEFT JOIN condition instead of the WHERE clause has a slightly different effect; it adds the condition to the join, instead of the filtering. This means that rows can be excluded based on something in the left table, regardless of whether you need the actual row (this is why ord.stat ended up in the LEFT JOINs). INNER JOINs would technically behave the same way, but it isn't usually noticeable because causing the right table to be excluded also excludes the left table.
于 2013-01-11T17:24:20.120 回答
0

我认为这应该可以解决问题,因为各个查询按预期工作:

SELECT sum1.tot + sum2.tot 
FROM  
(SELECT SUM(opn3.samt) as tot
FROM PUB.ord ord3, PUB.opn opn3
WHERE
ord3.subsnum = opn3.subsnum
AND ord3.onum = opn3.onum
AND ord3.DM1 != ''
AND ord3.DM1 IN('XCWAJC25','WCWAMO73') 
AND ord3.prdcde = 'CSC' 
AND ord3.stat != 16
AND opn3.samt >= 0
GROUP BY ord3.DM1, ord3.prdcde) sum1,

(SELECT SUM((-1 * opn2.samt) + ord2.samt) as tot
FROM PUB.ord ord2, PUB.opn opn2
WHERE
ord2.subsnum = opn2.subsnum
AND ord2.onum = opn2.onum
AND ord2.DM1 != ''
AND ord2.DM1 IN('XCWAJC25','WCWAMO73') 
AND ord2.prdcde = 'CSC' 
AND ord2.stat = 16
AND opn2.samt < 0
GROUP BY ord2.DM1, ord2.prdcde) sum2
于 2013-01-11T16:24:14.677 回答