我试图在两种不同的条件下获取相同变量的总和。只是想知道是否有办法将这些加在一起。查询的详细信息实际上并不太相关。基本上只是尝试取第一笔金额并加上第二笔金额。
例如 -
第一次查询:
select sum(amt_tot)
from agbgift, aprdclb
where agbgift_id = '1' and agbgift_id = aprdclb_id
第二次查询:
select sum(amt_tot)
from agbgift, aprxref, aprdclb where
aprxref_id = '1' and
agbgift_id = aprxref_xref_id and
aprxref_id = aprdclb_id and
xref_code in ('SPS','BUS','ORG','OWN','FDN' );
我要找的最终结果是' First Query Sum' +'第二个查询总和'
答案 0 :(得分:4)
在这种情况下,它就像这样简单:
SELECT
/* Query 1 (in parentheses) */
(select sum(amt_tot)
from agbgift, aprdclb
where agbgift_id = '1' and agbgift_id = aprdclb_id)
+ /* plus */
/* Query 2 (also in parentheses) */
(select sum(amt_tot)
from agbgift, aprxref, aprdclb where
aprxref_id = '1' and
agbgift_id = aprxref_xref_id and
aprxref_id = aprdclb_id and
xref_code in ('SPS','BUS','ORG','OWN','FDN' ))
答案 1 :(得分:-1)
with子句使其变得简单,如果不起作用,则可以在from子句
中使用别名with b ( select sum(amt_tot) as bsum
from agbgift, aprxref, aprdclb where
aprxref_id = '1' and
agbgift_id = aprxref_xref_id and
aprxref_id = aprdclb_id and
xref_code in ('SPS','BUS','ORG','OWN','FDN' ))
, a (select sum(amt_tot) asum
from agbgift, aprdclb
where agbgift_id = '1' and agbgift_id = aprdclb_id )
select a.asum + b.bsum
from a, b
在这种情况下确实如此:
select a.asum + b.bsum
from (select sum(amt_tot) asum
from agbgift, aprdclb
where agbgift_id = '1' and agbgift_id = aprdclb_id) as a,
( select sum(amt_tot) as bsum
from agbgift, aprxref, aprdclb where
aprxref_id = '1' and
agbgift_id = aprxref_xref_id and
aprxref_id = aprdclb_id and
xref_code in ('SPS','BUS','ORG','OWN','FDN' )) as b