本文介紹了對 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 條件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!