Applying OR condition on SUM function in SQL Server(对 SQL Server 中的 SUM 函数应用 OR 条件)
问题描述
我有 2 列名为 Debit 和 Credit.我想从一列中获取值并将其放入第三列 Balance.我想应用一个条件,如果 Debit 包含任何值,它应该放在 Balance 列中,如果 Credit 有东西,那么它应该在列中插入那个值,但如果两者都有一些值,那么只有一个应该去那里,Debit 或 Credit.
I have 2 columns named Debit and Credit. I want to get the value from one column and put in the third column, Balance. I want to apply a condition that if Debit contains any value, it should be put in the Balance column, and if Credit has something, then it should insert that value in the column, but if both have some values in them, then only one should go there, either Debit or Credit.
Debit Credit Balance
------------------------------
1000 NULL 1000
2200 NULL 2200
NULL 3000 3000
1500 1500 1500
查询:
SELECT
Debit, Credit, SUM(Credit|Debit) AS Balance
FROM Table
推荐答案
比如COALESCE()就够了
SELECT Debit, Credit, COALESCE(Credit, Debit) AS Balance
FROM Table
这篇关于对 SQL Server 中的 SUM 函数应用 OR 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对 SQL Server 中的 SUM 函数应用 OR 条件
基础教程推荐
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
