SQL,如何对两个不同查询的两个和求和

mar*_*630 0 mysql sql sum

我试图在两个不同的条件下获取相同变量的总和。只是想知道是否有办法将它们加在一起。查询的细节并不是很相关。基本上只是尝试将第一个和与第二个和相加。

例如 -

首先查询:

  select sum(amt_tot)
  from agbgift, aprdclb
  where agbgift_id = '1' and agbgift_id = aprdclb_id  
Run Code Online (Sandbox Code Playgroud)

第二个查询:

  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' );
Run Code Online (Sandbox Code Playgroud)

我正在寻找的最终结果是“第一查询总和” +“第二查询总和”

Gol*_*rol 5

在这种情况下,就这么简单:

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' ))
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但是这样简单吗?那是完全一样的。 (3认同)