oracle 加减多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12202100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:05:05  来源:igfitidea点击:

Adding and subtracting multiple columns

sqloracle

提问by user803860

I have 3 columns in a table, all the columns hold numeric values.

我在一个表中有 3 列,所有列都包含数值。

I want to add column a and b and minus column c what would the SQL for that be?

我想添加列 a 和 b 并减去列 c 的 SQL 是什么?

回答by Taryn

Unless I am missing something, it sounds like you just need to do this:

除非我遗漏了什么,听起来你只需要这样做:

select (NVL(cola, 0) + NVL(colb, 0)) - NVL(colc, 0)
from yourtable

回答by Bob Jarvis - Reinstate Monica

Responding to the question you posed in your comment to @bluefeet's answer:

回应您在评论中对@bluefeet 的回答提出的问题:

SELECT CASE
         WHEN A = B + C THEN A - C
         ELSE A + B - C
       END AS RESULT_VAL
  FROM SOME_TABLE

or you could just simply return B in the first case:

或者你可以在第一种情况下简单地返回 B :

SELECT CASE
         WHEN A = B + C THEN B
         ELSE A + B - C
       END AS RESULT_VAL
  FROM SOME_TABLE

Share and enjoy.

分享和享受。

回答by Ajay

Please check the below logic to subtract, implement the same using addition of multiple columns

请检查以下逻辑进行减法,使用多列相加来实现相同的

select 
SUM(a.COL1 -       B.COL1) COL1,
SUM(a.COL2 -       B.COL2) COL2,
SUM(a.COL3 -  B.COL3) COL3,
SUM(a.COL4 - B.COL4) COL4,
SUM(a.COL5 -       B.COL5) COL5 ,
SUM(a.COL6 -       B.COL6) COL6,
SUM(a.COL7-    B.COL7) COL7
FROM 
(select Coln, --Common column
sum(COL1) COL1 ,
sum(COL2)  COL2,
sum(COL3) COL3,
sum(COL4) COL4,
sum(COL5) COL5,
sum(COL6) COL6,
sum(COL7) COL7
from wrk_prod
GROUP BY Coln) a,
(select Coln, --Common column
sum(COL1) COL1 ,
sum(COL2)  COL2,
sum(COL3) COL3,
sum(COL4) COL4,
sum(COL5) COL5,
sum(COL6) COL6,
sum(COL7) COL7
from wrk_test
GROUP BY Coln) b
where a.Coln = b.Coln;

Thanks Ajay

谢谢阿杰