oracle 使用nullif解决除数为零的问题

先来说一下nullif的语法。
这里写图片描述

NULLIF compares expr1 and expr2. If they are equal, then the function returns null. If they are not equal, then the function returns expr1. You cannot specify the literal NULL for expr1.

If both arguments are numeric data types, then Oracle Database determines the argument with the higher numeric precedence, implicitly converts the other argument to that data type, and returns that data type. If the arguments are not numeric, then they must be of the same data type, or Oracle returns an error.

The NULLIF function is logically equivalent to the following CASE expression:
CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END

如果两个参数相等,返回null,否则返回第一个。第一个参数不可指定为空。对于非数字类型参数,数据类型必须一致。对于数值数据类型,会隐式的转化为更高优先级的数据类型。(这个理解可能有误,我测试了int,integer,float。但是最终都转化为number类型)。

一般来说,我们处理“除数为零”的问题会用到decode(当然也可以用case,但是写起来代码更多些)。比如

dividend / decode(divisor, 0, null, divisor)

但是在除数divisor非常复杂的时候,就需要把这一大串代码写两遍,或者是再套一层view。无论是哪种,都是极其不利于维护的。

1 / 
decode((sum(t.val) over(order by t.c) +
       nvl(lead(val) over(partition by b order by c), 0)) /
      sum(val) over(partition by b order by c),
      0,
      null,
      (sum(t.val) over(order by t.c) +
       nvl(lead(val) over(partition by b order by c), 0)) / sum(val)
      over(partition by b order by c))

对于这种复杂表达式的除数,每回修改都要改两遍,很容易出错。

利用nullif,可以让除数只写一次。
因为 decode(divisor, 0, null, divisor)nullif(divisor, 0) 是等效的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值