我正在画一个空白,我的查询完美无缺,但现在我只想返回
TotalCredits - TotalDebits as Difference
下面是产生我需要减去的两个值的查询,我用连接玩了但是我觉得我偏离了轨道。
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
答案 0 :(得分:3)
尝试这样的事情
SELECT
SUM(CASE WHEN CREDIT = 1 THEN TOTALAMOUNT END) as TotalCredits,
SUM(CASE WHEN DEBIT = 1 THEN TOTALAMOUNT END) as TotalDebits,
SUM(CASE WHEN CREDIT = 1 THEN TOTALAMOUNT END) - SUM(CASE WHEN DEBIT = 1 THEN TOTALAMOUNT END) as Diff
FROM journal
WHERE memberid=48
答案 1 :(得分:1)
将您的查询放入这样的派生表中;
select TotalCredits - TotalDebits as Difference
from
(
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
) temp