即使使用唯一身份证也无法获得总计

时间:2016-02-16 11:27:18

标签: sql oracle

- 以下是我的查询&我希望将2个费用代码组合在一起 - 我尝试过分析函数并得到了相同的结果

WITH t AS
  (SELECT quote_section_id,
    expense_id,
    Quoted Quoted,
    NULL procured
  FROM VW_PM_SUM_Quoted
  WHERE VW_PM_SUM_Quoted.QUote_section_id = '1000065052'
  UNION
  SELECT quote_section_id ,
    expense_id ,
    NULL Quoted ,
    procured
  FROM VW_PM_SUM_procured
  WHERE VW_PM_SUM_procured.QUote_section_id = '1000065052'
  )
select quote_section_id, expense_id, quoted, procured
from t

- 结果

Quote_section_id expense_id quoted procured 
1000065052      1000000547   10320
1000065052      1000000547           825.9
1000065052      1000000571     546  
1000065052      1000000582          2201.43
1000065052      1000000619  102297  

- 期望的结果

Quote_section_id expense_id quoted  procured balance 
1000065052      1000000547   10320  825.9    9494.1     
1000065052      1000000571     546            546
1000065052      1000000582          2201.43  -2201.43
1000065052      1000000619  102297           102297

2 个答案:

答案 0 :(得分:1)

看来你只是在寻找一个联盟。在这种情况下的完全外部联接,其中一个或两个表都有费用ID的数据。

select 
  quote_section_id,
  expense_id,
  q.quoted,
  p.procured,
  nvl(q.quoted, 0) - nvl(p.procured, 0) as balance
from vw_pm_sum_quoted q
full outer join vw_pm_sum_procured p using (quote_section_id, expense_id)
where quote_section_id = '1000065052'
order by quote_section_id, expense_id;

答案 1 :(得分:0)

我猜你正在寻找的是

WITH t AS
  (SELECT quote_section_id,
    expense_id,
    Quoted Quoted,
    NULL procured
  FROM VW_PM_SUM_Quoted
  WHERE VW_PM_SUM_Quoted.QUote_section_id = '1000065052'
  UNION
  SELECT quote_section_id ,
    expense_id ,
    NULL Quoted ,
    procured
  FROM VW_PM_SUM_procured
  WHERE VW_PM_SUM_procured.QUote_section_id = '1000065052'
  )
select quote_section_id, expense_id, Sum(quoted), sum(procured), sum(nvl(quoted,0) - nvl(procured,0)) balance
from t
group by quote_section_id, expense_id