TSQL - 从两个表中获取总和或减去

时间:2016-03-21 10:27:53

标签: sql-server tsql

我想要总结/减少销售价值'从我的程序中的两个表。销售1有收据,第二是有退货。但我迷失在想法中。

    SELECT *
    FROM @possale1

    SELECT *
    FROM @possale2

    SELECT sum(salevalue) AS S1
    FROM @possale1

    SELECT sum(salevalue)*-1 AS S2
    FROM @possale2

    select sum(sum(a.salevalue)-sum(b.salevalue)) 
    from @possale1 a 
    inner join @possale2 b on a.receiptdate=b.receiptdate

2 个答案:

答案 0 :(得分:1)

下一步应该没有聚合:

select ((SELECT sum(salevalue) FROM @possale1) - (SELECT sum(salevalue) FROM @possale2)) as balance

答案 1 :(得分:0)

你在尝试这个吗?

SELECT SUM(ISNULL(a.salevalue,0) - ISNULL(b.salevalue,0))
FROM @possale1 a FULL OUTER JOIN @possale2 b on a.receiptdate=b.receiptdate