gpt4 book ai didi

sql - 减去两列为空

转载 作者:行者123 更新时间:2023-12-03 02:02:49 25 4
gpt4 key购买 nike

我使用以下内容

 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

这会返回一个与我不同的字段,我遇到的问题是,如果表没有贷项,但有借项,则临时表的 TotalCredits 字段中包含一个 NULL 值,该值禁止进行数学运算。 (Vica Versa 上有积分,但没有借方)我已经尝试过合并,但似乎无法使其工作。

理性地我需要检查是否:

sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1 as TotalCredits is 
null then totalcredits = 0 and visa versa

SQL Server 2008

最佳答案

 select ISNULL(TotalCredits,0) - ISNULL(TotalDebits,0) 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

关于sql - 减去两列为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21737845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com