问题描述
我有 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