我想我无法正确地提出我的问题,所以我的 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