在SQL中汇总值

时间:2015-03-09 20:55:03

标签: sql-server

当我尝试在两个表中的两个字段之间获得合并剩余余额时出现问题

要求:仅根据income_o和outcome_o表计算所有出口的合并剩余余额。

我的查询是:

select (RE.inc-RE.out) AS  dif
FROM(
SELECT SUM(RESULTADO.inc) inc, SUM(RESULTADO.out) out
    FROM
    (
        SELECT point, date, null as out, inc
        FROM Income_o
        UNION ALL
        SELECT point, date, out, null as inc
        FROM Outcome_o
    ) RESULTADO
) RE

我的结果是:

28985.9600

,正确的结果是:

28985.9600

但是当我运行查询时,程序会告诉我,而不是我的查询不正确,但是我的结果是正确的

数据库架构的链接:http://img1.imagilive.com/affiche/0315/Snap_09-03-2015_at_144932.jpg.htm

1 个答案:

答案 0 :(得分:0)

你也可以这样试试。

SELECT SUM(t.IncOut) AS 'dif'
FROM (
    SELECT inc AS 'IncOut'
    FROM Income_o
    UNION ALL
    SELECT out * -1
    from Outcome_o
) t