对 SQL Server 中的 SUM 函数应用 OR 条件
- 作者: 丶浅陌丶
- 来源: 51数据库
- 2022-12-06
问题描述
我有 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
推荐阅读
热点文章
检查拆分键盘
0
带有“上一个"的工具栏和“下一个"用于键盘输入AccessoryView
0
Activity 启动时显示软键盘
0
UIWebView 键盘 - 摆脱“上一个/下一个/完成"酒吧
0
在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画
0
我的 iOS 应用程序中的键盘在 iPhone 6 上太高了.如何在 XCode 中调整键盘的分辨率?
0
android:inputType="textEmailAddress";- '@' 键和 '.com' 键?
0
禁用 iPhone 中键盘的方向
0
Android 2.3 模拟器上的印地语键盘问题
0
keyDown 没有被调用
0
