SQL 使用解码检查负值和正值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14037334/
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-01 12:48:48  来源:igfitidea点击:

Using decode to check for negative and positive values

sql

提问by user1872384

Hi is there a way to use decode to check for positive and negative values in sql?

嗨,有没有办法使用解码来检查 sql 中的正值和负值?

e.g.

例如

select decode(money_return, **<0**, abs(money_return), **>0**, 
money_return*10, money_return) from cash_t;

if the logic is coded in if else statement it will be something like this:

如果逻辑在 if else 语句中编码,它将是这样的:

if(money_return<0){money_reutrn = abs(money_return);}
else if(money_return>0){money_reutrn = money_return*10;}
else {money_return = money_return;}
end

Thank you.

谢谢你。

采纳答案by rs.

You need case statement

你需要case语句

select CASE 
 WHEN money_return < 0 THEN abs(money_return)
 WHEN money_return > 0 THEN money_return*10
 ELSE money_return END money_return from cash_t;

回答by Damienknight

You can use 'sign' with a decode. Sign will return -1 for any negative, 0 for 0, and 1 for any positive:

您可以在解码中使用“符号”。对于任何负数,Sign 将返回 -1,0 为 0,任何正数为 1:

select decode(sign(money_return), -1, abs(money_return), 
                                   1, money_return*10, 
                                   money_return) 
from cash_t;

回答by ?????? ?????

I use function "greatest" and "least" found Positive values or Negative value.

我使用函数“最大”和“最少”找到正值或负值。

example.

例子。

select decode(greatest(value,0), 0,'Negative','Positive') from dual

select decode(greatest(value,0), 0,'Negative','Positive') from dual

or

或者

select decode(least(value,0), 0,'Positive','Negative') from dual

select decode(least(value,0), 0,'Positive','Negative') from dual

thank

谢谢

回答by Volker Bach

Use SIGN(). It returns "0" for 0, "1" for positive and "-1" for negative values.

使用 SIGN()。它为 0 返回“0”,为正值返回“1”,为负值返回“-1”。

SELECT
    DECODE(
        SIGN(money_return),
        0,
        money_return,
        1,
        money_return * 10,
        - 1,
        ABS(money_return)
    )
FROM
    CASH_T;

回答by Mari

You can use as like this,

你可以像这样使用,

SELECT supplier_name,
decode(supplier_id, 10000, 'IBM',
                    10001, 'Microsoft',
                    10002, 'Hewlett Packard',
                    'Gateway') result
FROM suppliers;

The above decode statement is equivalent to the following IF-THEN-ELSE statement:

上面的 decode 语句等价于下面的 IF-THEN-ELSE 语句:

IF supplier_id = 10000 THEN
   result := 'IBM';

ELSIF supplier_id = 10001 THEN
   result := 'Microsoft';

ELSIF supplier_id = 10002 THEN
   result := 'Hewlett Packard';

ELSE
   result := 'Gateway';

END IF;

So your final code should be something like this,

所以你的最终代码应该是这样的,

select decode(money_return, money_return<0, abs(money_return),
                            money_return>0, money_return*10,
                                            money_return) result
from cash_t;

回答by SWeko

If that is the only formula you use, you could do:

如果这是您使用的唯一公式,您可以这样做:

select (11 *abs(money_return) + 9 *money_return)/2 as money_return
from cash_t

SQL Fiddleto check the results.

SQL Fiddle检查结果。

Now, this is very, very fragile, and won't work if the scenario changes.

现在,这非常非常脆弱,如果场景发生变化,它将无法工作。

回答by Nipun Jain

select CASE money_return  
  WHEN money_return < 0 THEN abs(money_return) 
  WHEN money_return > 0 THEN money_return*10
  ELSE money_return END money_return from cash_t;

回答by CloseISQ

SELECT
    decode(
        money_return - Abs(money_return),
        0,
        money_return * 10,
        abs(money_return)
    )
FROM
    cash_t;

If the value is positive, deducting it from itself will return 0 and the first condition will be applied. Else, it is negative and the second condition is applied. 0 Will fit into the first condition and return 0 (as 0*10).

如果该值为正,则从自身中减去该值将返回 0 并应用第一个条件。否则,它是否定的,并应用第二个条件。0 将适合第一个条件并返回 0(作为 0*10)。