在具有SUM计算的嵌套查询中使用UNION

时间:2013-07-04 08:59:18

标签: sql

我想将此查询的总和显示为仅一个单元格,但此处结果显示为此总计

的列
  500
  1000

但我想显示这些记录的总和(1500)

这是我的疑问:

select sum(MAL_TASWYAHESAB.MADINMONY) as'Total Sum' from MAL_TASWYAHESAB  
union 
select sum(MAL_AMRSARF_OHAD.MONY) as'Total Sum' from MAL_AMRSARF_OHAD

4 个答案:

答案 0 :(得分:1)

尝试这种方式:

select (select sum(MAL_TASWYAHESAB.MADINMONY) from MAL_TASWYAHESAB)
+
(select sum(MAL_AMRSARF_OHAD.MONY) from MAL_AMRSARF_OHAD) as 'Total Sum'

答案 1 :(得分:0)

SELECT SUM(TotalSum) AS Total
FROM
(
    select MAL_TASWYAHESAB.MADINMONY as 'TotalSum' from MAL_TASWYAHESAB  
    union 
    select MAL_AMRSARF_OHAD.MONY                   from MAL_AMRSARF_OHAD
) AS t

答案 2 :(得分:0)

select sum(MADINMONY) as'Total Sum'
from (
    select MAL_TASWYAHESAB.MADINMONY
    from MAL_TASWYAHESAB
    union MAL_AMRSARF_OHAD.MONY
    from MAL_AMRSARF_OHAD
) AS X

答案 3 :(得分:0)

SELECT SUM(MADINMONY) as'Total Sum'
FROM (
    SELECT MAL_TASWYAHESAB.MADINMONY
    FROM MAL_TASWYAHESAB
    UNION MAL_AMRSARF_OHAD.MONY
    FROM MAL_AMRSARF_OHAD
) AS a;