2013-01-11 54 views
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 

感謝

回答

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 != '',因爲值必須在特定的設置
  • 把一個條款爲LEFT JOIN條件,而不是WHERE子句有一個略有不同的效果;它將條件添加到連接中,而不是過濾。這意味着可以根據左表中的某些內容排除行,而不管您是否需要實際行(這就是爲什麼ord.statLEFT JOIN爲結尾)。 INNER JOIN s在技術上會表現相同的方式,但通常不會引起注意,因爲排除右表也會排除左表。
+0

親愛的上帝你已經做到了! OMG,非常感謝。你已經在一個荒謬統治的國家讓一個腦死亡的程序員非常高興! – PolishHurricane

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