我想我无法正确表达我的问题所以这就是我的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
答案 0 :(得分: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;
答案 1 :(得分: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
答案 2 :(得分:0)
查询:
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