Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Divide by Zero Error encountered in Snowflake Query

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
like image 672
user3369545 Avatar asked Mar 02 '23 10:03

user3369545


1 Answers

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

like image 163
Dean Flinter Avatar answered May 16 '23 07:05

Dean Flinter