1

我想我无法正确地提出我的问题,所以我的 SQL 问题出现了。我运行以下查询:

SELECT P.ORDER_NO, P.LINE_NO, P.RELEASE_NO, SUM(P.BUY_QTY_DUE) 
FROM PURCHASE_ORDER_LINE_ALL P
WHERE P.CONTRACT='APC32' AND P.PART_NO='900-0089' AND ORDER_NO='38288'
GROUP BY P.ORDER_NO, P.LINE_NO, P.RELEASE_NO;

结果变为:

ORDER_NO LINE_NO RELEASE_NO SUM(P.BUY_QTY_DUE)
38288    1       1          140

第二个查询是:

SELECT R.ORDER_NO, R.LINE_NO, R.RELEASE_NO, SUM(R.INV_QTY_ARRIVED)
FROM PURCHASE_RECEIPT_NEW R
WHERE R.ORDER_NO='38288' AND R.LINE_NO='1' AND R.RELEASE_NO='1' 
GROUP BY R.ORDER_NO, R.LINE_NO, R.RELEASE_NO;

结果:

ORDER_NO LINE_NO RELEASE_NO SUM(R.INV_QTY_ARRIVED)
38288    1       1          140

如果我使用以下结果显然是错误的。我怎样才能得到这样的结果:

ORDER_NO LINE_NO RELEASE_NO SUM(P.BUY_QTY_DUE) SUM(R.INV_QTY_ARRIVED)
38288    1       1          140                140

 

SELECT P.ORDER_NO, P.LINE_NO, P.RELEASE_NO, SUM(P.BUY_QTY_DUE), SUM(R.INV_QTY_ARRIVED)
FROM 
PURCHASE_ORDER_LINE_ALL P
LEFT JOIN PURCHASE_RECEIPT_NEW R
ON P.ORDER_NO=R.ORDER_NO AND P.LINE_NO=R.LINE_NO AND P.RELEASE_NO=R.RELEASE_NO
WHERE P.CONTRACT='APC32' AND P.PART_NO='900-0089' AND R.ORDER_NO='38288' AND R.LINE_NO='1' AND R.RELEASE_NO='1' 
GROUP BY P.ORDER_NO, P.LINE_NO, P.RELEASE_NO;

错误的结果,排序:

ORDER_NO LINE_NO RELEASE_NO SUM(P.BUY_QTY_DUE) SUM(R.INV_QTY_ARRIVED)
38288    1       1          420                140
4

3 回答 3

1

将两者分组,然后加入他们。

select pal.order_no,pal.line_no,pal.release_no,pal.sum_buy_qty_due,prn.sum_inv_qty_arrived
  from (select P.order_no,P.line_no,P.release_no,
               sum(P.buy_qty_due) sum_buy_qty_due
          from purchase_order_line_all P
         where P.contract = 'APC32'
           and P.part_no = '900-0089'
           and order_no = '38288'
         group by P.order_no,P.line_no,P.release_no) pal
       inner join (select R.order_no,R.line_no,R.release_no,
                          sum(R.inv_qty_arrived) sum_inv_qty_arrived
                     from purchase_receipt_new R
                    where R.order_no = '38288'
                      and R.line_no = '1'
                      and R.release_no = '1'
                    group by R.order_no,R.line_no,R.release_no) prn
               on prn.order_no = pal.order_no 
                  and prn.line_no = pal.line_no 
                  and prn.release_no = pal.release_no 

例如:http ://sqlfiddle.com/#!4/87d9b/1

于 2013-02-28T12:47:54.813 回答
1
SELECT P.ORDER_NO, P.LINE_NO, P.RELEASE_NO, SUM(P.BUY_QTY_DUE) 
FROM PURCHASE_ORDER_LINE_ALL P
WHERE P.CONTRACT='APC32' AND P.PART_NO='900-0089' AND ORDER_NO='38288'
GROUP BY P.ORDER_NO, P.LINE_NO, P.RELEASE_NO;
UNION/UNION ALL
SELECT 0, 0, 0,0, SUM(R.INV_QTY_ARRIVED)
FROM PURCHASE_RECEIPT_NEW R
WHERE R.ORDER_NO='38288' AND R.LINE_NO='1' AND R.RELEASE_NO='1' 
GROUP BY R.ORDER_NO, R.LINE_NO, R.RELEASE_NO;
于 2013-02-28T12:09:16.840 回答
0

Query:

SELECT P.ORDER_NO,
       P.LINE_NO,
       P.RELEASE_NO,
       SUM(P.BUY_QTY_DUE) AS  SUMBUY_QTY_DUE,
       (SELECT SUM(R.INV_QTY_ARRIVED)
        FROM PURCHASE_RECEIPT_NEW R
        WHERE R.ORDER_NO=P.ORDER_NO
          AND R.LINE_NO=P.LINE_NO
          AND R.RELEASE_NO=P.RELEASE_NO) AS SUMINV_QTY_ARRIVED
FROM PURCHASE_ORDER_LINE_ALL P
WHERE P.CONTRACT='APC32'
  AND P.PART_NO='900-0089'
  AND ORDER_NO='38288'
GROUP BY P.ORDER_NO,
         P.LINE_NO,
         P.RELEASE_NO
于 2013-02-28T12:55:33.187 回答