通过联接复制Oracle SQL结果

时间:2018-11-21 11:45:51

标签: oracle

我正在尝试使用以下代码:

SELECT a.part_no,
       sum(a.BUY_QTY_DUE) AS Tot_Ord,
       sum(b.REVISED_QTY_DUE) AS S_O_Tot
FROM CUSTOMER_ORDER_JOIN a
LEFT OUTER JOIN SHOP_ORD b ON a.part_no= b.part_no
AND b.contract = '20'
AND b.state = 'Planned'
WHERE a.PART_NO = '10002261'
  AND a.OBJSTATE = 'Released'
  AND a.CONTRACT = '10'
GROUP BY a.part_no

但是我看到重复的结果,我知道这与联接有关,但不确定如何解决:( Results

我希望看到的结果是

Tot_Ord = 277
(23个实例6个,46个实例3个,1个实例1个)共10行

S_O_Tot = 46
(2个实例,共23个)总计2行

任何帮助将不胜感激,

谢谢

杰米

1 个答案:

答案 0 :(得分:0)

SELECT a.part_no,
sum(a.BUY_QTY_DUE) AS Tot_Ord,
sum(nvl(b.REVISED_QTY_DUE,0)) AS S_O_Tot
FROM CUSTOMER_ORDER_JOIN a
LEFT OUTER JOIN SHOP_ORD b ON (a.part_no= b.part_no
AND b.contract = '20'
AND b.state = 'Planned')
WHERE a.PART_NO = '10002261'
AND a.OBJSTATE = 'Released'
AND a.CONTRACT = '10'

我已删除分组,因为您仍然要对此文件进行过滤