最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

oracle使用nullif解決除數(shù)為零的問(wèn)題分析

 更新時(shí)間:2025年11月10日 09:03:08   作者:nayi_224  
文章介紹了如何使用Oracle數(shù)據(jù)庫(kù)中的NULLIF函數(shù)來(lái)解決除數(shù)為零的問(wèn)題,NULLIF函數(shù)在兩個(gè)表達(dá)式相等時(shí)返回NULL,否則返回第一個(gè)表達(dá)式,這對(duì)于處理復(fù)雜的除數(shù)問(wèn)題非常有用,因?yàn)樗梢员苊庵貜?fù)代碼,提高可維護(hù)性

oracle使用nullif解決除數(shù)為零

說(shuō)一下nullif的語(yǔ)法


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

如果兩個(gè)參數(shù)相等,返回null,否則返回第一個(gè)。

第一個(gè)參數(shù)不可指定為空。對(duì)于非數(shù)字類型參數(shù),數(shù)據(jù)類型必須一致。對(duì)于數(shù)值數(shù)據(jù)類型,會(huì)隱式的轉(zhuǎn)化為更高優(yōu)先級(jí)的數(shù)據(jù)類型。(這個(gè)理解可能有誤,我測(cè)試了int,integer,float。但是最終都轉(zhuǎn)化為number類型)。

一般來(lái)說(shuō)

我們處理“除數(shù)為零”的問(wèn)題會(huì)用到decode(當(dāng)然也可以用case,但是寫起來(lái)代碼更多些)。

比如:

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

但是在除數(shù)divisor非常復(fù)雜的時(shí)候,就需要把這一大串代碼寫兩遍,或者是再套一層view。

無(wú)論是哪種,都是極其不利于維護(hù)的。

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))

對(duì)于這種復(fù)雜表達(dá)式的除數(shù),每回修改都要改兩遍,很容易出錯(cuò)。

利用nullif,可以讓除數(shù)只寫一次。

因?yàn)?decode(divisor, 0, null, divisor)nullif(divisor, 0) 是等效的。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

安康市| 泰兴市| 南川市| 禄丰县| 仙桃市| 梁山县| 泰安市| 崇礼县| 株洲市| 辽阳县| 治县。| 突泉县| 双流县| 琼结县| 抚宁县| 锡林郭勒盟| 若羌县| 清河县| 凌源市| 阿城市| 澄城县| 济阳县| 盐池县| 德清县| 闽侯县| 元谋县| 肇东市| 沁水县| 上思县| 天津市| 嘉善县| 陆川县| 定日县| 专栏| 鹿泉市| 阿拉尔市| 全州县| 松滋市| 八宿县| 常宁市| 德清县|