当前位置:网站首页>Demonstration of the processing of divisor 0 in SQL
Demonstration of the processing of divisor 0 in SQL
2022-06-24 04:04:00 【User 8989785】
When we make statistics , It is common to find the percentage , Chain ratio , Year on year and other situations that need to be divided by a certain number , And if the divisor is 0, The database will report errors . So how do we deal with this situation ? Let's use an example to explain the processing method .
Situation 1
for example
SELECT A/B FROM TAB
In this case , The general treatment is to use CASE WHEN To judge B Value
SELECT CASE WHEN B=0 THEN 0 ELSE A/B END FROM TAB
So when B If it is 0, Let's just assign a value , avoid A/B Participate in the calculation and report errors .
Situation two
The above is a common situation , But what if you encounter the following aggregate function ?
for example
SELECT SUM(A)/COUNT(B) FROM TAB
In this case CASE WHEN Can't judge COUNT(B) The value of the , because WHEN The following conditions cannot use aggregate functions ( Grammar requires ), At this time, we can deal with it like this
SELECT ISNULL(SUM(A)/NULLIF(COUNT(B),0),0) FROM TAB
Two functions are used here ,NULLIF() and ISNULL()
NULLIF Function has two arguments , The definition is as follows :
NULLIF( expression1 , expression2 )
Its function is : If two specified expressions are equal , Just go back to NULL value .
ISNULL The function also has two arguments , The definition is as follows :
ISNULL( expression1 , expression2 )
Its effect is : If the result of the first parameter is NULL, Just return the value of the second parameter .
When COUNT(B) As the result of the 0 when , Exactly the same as the second given parameter 0 equal , This is the time NULLIF The function will return NULL, and SUM(A) Divided by NULL The result is NULL, Outer use ISNULL Function realignment NULL Value for judgment , The end result is 0 了 .
These two methods are that we deal with the divisor as 0 The situation of the , Be sure to remember !
边栏推荐
- Prometheus pushgateway
- Two edges are applied by default, one of which is a solid color icon. How to delete that solid color icon?
- Notice on stopping maintenance of this column
- 618 promotion: mobile phone brand "immortal fight", high-end market "who dominates the ups and downs"?
- The use of char[0] and char[1] in C language structure
- Slide left from small window to large windowdispatchframelayout
- Installation of pytorch in pycharm
- Real time monitoring of water conservancy by RTU of telemetry terminal
- Why is on-line monitoring of equipment more and more valued by people?
- Modstartcms enterprise content site building system (supporting laravel9) v4.2.0
猜你喜欢
![[code Capriccio - dynamic planning] t392 Judgement subsequence](/img/59/9da6d70195ce64b70ada8687a07488.png)
[code Capriccio - dynamic planning] t392 Judgement subsequence

ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0

On game safety (I)

Pine Script脚本常用内容

Clickhouse (02) Clickhouse architecture design introduction overview and Clickhouse data slicing design

Black hat SEO practice: General 301 weight PR hijacking

Common content of pine script script

Halcon knowledge: contour operator on region (2)

Black hat SEO actual combat directory wheel chain generates millions of pages in batch

多任务视频推荐方案,百度工程师实战经验分享
随机推荐
SQL注入绕过安全狗思路一
A problem of testing security group access in windows10
Oceanbase community OBD deployment example primary replica
Rasa 3. X learning series -rasa 3.2.0 new release
618 promotion: mobile phone brand "immortal fight", high-end market "who dominates the ups and downs"?
Hprof information in koom shark with memory leak
[Numpy] Numpy对于NaN值的判断
flutter系列之:flutter中的offstage
给你讲懂 MVCC
ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0
Record a programming contest
Diskpart San policy is not onlineall, which affects automatic disk hanging
Slide left from small window to large windowdispatchframelayout
Several options of F8 are very useful
开源之夏2022中选结果公示,449名高校生将投入开源项目贡献
Have you learned all these routines to solve bugs?
Difference and efficiency between get winevent and get eventlog
Tencent cloud console work order submission Guide
Several good books for learning data
祝贺钟君成为 CHAOSS Metric Model 工作组的 Maintainer