DatabaseError: Execution failed on sql 'select * from(select plucode as "商品编码",pluname as "商品名称",sum(HXTotal) as"销售金额",round(sum(HXTotal)/(select sum(hxtotal) FROM tSalPluDetail202305 A WHERE RPTDATE='2023-05-29' AND exists(select 1 from torgexinfo where udp1='超市' and udp3<>'3-批发中心' and orgcode=a.orgcode) and substr(clscode,1,4)='0802' )*100,2) as "销售占比",sum(HMTotal) as "毛利",round(sum(HMTotal)/sum(HXTotal)*100,2) as "毛利率"FROM tSalPluDetail202305 A WHERE RPTDATE='2023-05-29' AND exists(select 1 from torgexinfo where udp1='超市' and udp3<>'3-批发中心' and orgcode=a.orgcode)and substr(clscode,1,4)='0802' group by plucode,pluname ORDER BY sum(HXTotal) DESC) where ROWNUM <= 20': ORA-01476: 除数为 0
时间: 2024-02-20 15:00:51 浏览: 20
这个错误是由于在SQL语句中,除数为0导致的。具体地说,在SQL语句的这一部分中:
```
round(sum(HXTotal)/(select sum(hxtotal) FROM tSalPluDetail202305 A WHERE RPTDATE='2023-05-29' AND exists(select 1 from torgexinfo where udp1='超市' and udp3<>'3-批发中心' and orgcode=a.orgcode) and substr(clscode,1,4)='0802' )*100,2) as "销售占比"
```
我们计算了销售占比,其中分子是销售金额的总和,分母是某些过滤条件下的销售金额总和。但是,如果分母为0,就会导致除数为0的错误。
要解决这个问题,我们需要在SQL语句中加入一个判断,避免分母为0的情况。一种简单的方法是使用`NULLIF`函数,将分母为0的情况转化为NULL:
```
round(sum(HXTotal)/NULLIF((select sum(hxtotal) FROM tSalPluDetail202305 A WHERE RPTDATE='2023-05-29' AND exists(select 1 from torgexinfo where udp1='超市' and udp3<>'3-批发中心' and orgcode=a.orgcode) and substr(clscode,1,4)='0802' ),0)*100,2) as "销售占比"
```
这样,如果分母为0,就会返回NULL,而不是导致除数为0的错误。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)