在 Snowflake 查询中遇到除以零错误
Divide by Zero Error encountered in Snowflake Query
我有以下雪花查询,其中出现除以零错误...你能帮我吗...
with cte1 as
(select * from "coe.cup"
where typeofcare ='AM'
and status ='DONE'
and review ='false'
and date (assigneddate)>='2021-04-01'), cte2 as(
select cast(completed as date) completeddate ,iscode
,iff(iscode=1,datediff(minute,assigneddate,coded),0) codeddatetime
,iff(iscode=0,datediff(minute,assigneddate,qaed),0) qaeddatetime
,datediff(minute,assigneddate,completed) overall from
(select *,iff(qaed='1900-01-01 00:00:00.0000000',1,0) iscode from cte1)a )
select completeddate
,sum(iff(iscode=1,1,0)) noofvisitbillscoded
,sum(iff(iscode=1,0,1)) noofvisitbillscodedandqaed
,count(1) totalvisitbillscompleted
,cast(sum(codeddatetime)/sum(iff(iscode=1,1,0)) as float)/60 averagetimeforcodedvisitbills
,cast(sum(qaeddatetime)/sum(iff(iscode=1,0,1)) as float)/60 averagetimeforcodedandqaedvisitbills
,cast(sum(overall)/count(1) as float)/60 overallaveragetime
from cte2
group by completeddate
我假设这是由于 /sum(iff(iscode=1,1,0))
而发生的,其中可能有时 returns 0.
处理被零除的一种方法是使用NULLIF
NULLIF( <expr1> , <expr2> )
returns NULL
如果 expr1
等于 expr2
,否则 returns expr1
.
因此,在您的代码中,例如 sum(iff(iscode=1,1,0))
,您可以将其替换为:
NULLIF(sum(iff(iscode = 1, 1, 0)), 0)
然后这应该 return NULL
而不是导致被零除。
另一种选择是使用 DIV0 函数,例如:
DIV0(sum(codeddatetime),sum(iff(iscode=1,1,0))
更多信息在这里:https://docs.snowflake.com/en/sql-reference/functions/div0.html
当涉及除法时,除数可以用 NULLIFZERO:
处理
NULLIFZERO( )
Returns NULL if the argument evaluates to 0; otherwise, returns the argument.
第二个模式 sum(iff(iscode=1,1,0))
是模拟过滤后的 COUNT 的条件总和。它可以进一步简化 COUNT_IF
COUNT_IF( )
Returns the number of records that satisfy a condition.
总结一下:
,cast(sum(codeddatetime)/sum(iff(iscode=1,1,0)) as float)/60
=>
,SUM(codeddatetime)/NULLIFZERO(COUNT_IF(iscode=1))/60
=> if iscode is boolean column then:
,SUM(codeddatetime)/NULLIFZERO(COUNT_IF(iscode))/60
我有以下雪花查询,其中出现除以零错误...你能帮我吗...
with cte1 as
(select * from "coe.cup"
where typeofcare ='AM'
and status ='DONE'
and review ='false'
and date (assigneddate)>='2021-04-01'), cte2 as(
select cast(completed as date) completeddate ,iscode
,iff(iscode=1,datediff(minute,assigneddate,coded),0) codeddatetime
,iff(iscode=0,datediff(minute,assigneddate,qaed),0) qaeddatetime
,datediff(minute,assigneddate,completed) overall from
(select *,iff(qaed='1900-01-01 00:00:00.0000000',1,0) iscode from cte1)a )
select completeddate
,sum(iff(iscode=1,1,0)) noofvisitbillscoded
,sum(iff(iscode=1,0,1)) noofvisitbillscodedandqaed
,count(1) totalvisitbillscompleted
,cast(sum(codeddatetime)/sum(iff(iscode=1,1,0)) as float)/60 averagetimeforcodedvisitbills
,cast(sum(qaeddatetime)/sum(iff(iscode=1,0,1)) as float)/60 averagetimeforcodedandqaedvisitbills
,cast(sum(overall)/count(1) as float)/60 overallaveragetime
from cte2
group by completeddate
我假设这是由于 /sum(iff(iscode=1,1,0))
而发生的,其中可能有时 returns 0.
处理被零除的一种方法是使用NULLIF
NULLIF( <expr1> , <expr2> )
returns NULL
如果 expr1
等于 expr2
,否则 returns expr1
.
因此,在您的代码中,例如 sum(iff(iscode=1,1,0))
,您可以将其替换为:
NULLIF(sum(iff(iscode = 1, 1, 0)), 0)
然后这应该 return NULL
而不是导致被零除。
另一种选择是使用 DIV0 函数,例如:
DIV0(sum(codeddatetime),sum(iff(iscode=1,1,0))
更多信息在这里:https://docs.snowflake.com/en/sql-reference/functions/div0.html
当涉及除法时,除数可以用 NULLIFZERO:
处理NULLIFZERO( )
Returns NULL if the argument evaluates to 0; otherwise, returns the argument.
第二个模式 sum(iff(iscode=1,1,0))
是模拟过滤后的 COUNT 的条件总和。它可以进一步简化 COUNT_IF
COUNT_IF( )
Returns the number of records that satisfy a condition.
总结一下:
,cast(sum(codeddatetime)/sum(iff(iscode=1,1,0)) as float)/60
=>
,SUM(codeddatetime)/NULLIFZERO(COUNT_IF(iscode=1))/60
=> if iscode is boolean column then:
,SUM(codeddatetime)/NULLIFZERO(COUNT_IF(iscode))/60