I have the following snowflake query where I am getting a divide by zero error...Can you please help me here..
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
Another option is to to use the DIV0 function so something like:
DIV0(sum(codeddatetime),sum(iff(iscode=1,1,0))
More info here: https://docs.snowflake.com/en/sql-reference/functions/div0.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With