我不确定错误,因为我正在尝试运行此查询。它给了我零除错误。此错误发生在第1行。
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
有人可以帮忙吗?
SELECT temp1.*,90 as Plan_val,max(cast(Round(temp2.actual,0) as int)) as actual_val FROM(
SELECT t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)] , REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id FROM (
SELECT 'NOC Incident Resolution - High' as [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)] , REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id
FROM TEMP_TICKET_STATE
UNION
SELECT 'NOC Incident Resolution - High' as [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)] , REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id
FROM TEMP_TICKET_STATE
where [Problem_Type_Name(Parent)]='Incident (Technical Issues)' and PRIORITY_TYPE_NAME in ('High') AND (ISNULL([Escalated to Closed time],0) <= 120 or ISNULL([Escalated to Resolved time],0) <=120)) t
group by t.[Problem_Type_Name(Parent)] ,t.[Problem_Type_Name(Child)] , REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id) temp1
INNER JOIN
(
SELECT t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)],0 AS plan_val, cast(Round((cast(sum(t.Total_tickets) as decimal(38,2))- cast(sum(t.Alarm_Val) as decimal(38,2)))/cast(sum(t.Total_tickets) as decimal(38,2))*100,0) as int) as actual FROM (
SELECT 'NOC Incident Resolution - High' as [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)],0 as Alarm_Val, cast (count(distinct [Job_ticket_id])/4 as int) as Total_tickets
FROM TEMP_TICKET_STATE_month
group by [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)]
UNION
SELECT 'NOC Incident Resolution - High' as [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)],cast (count(distinct [Job_ticket_id])/4 as int) as Alarm_Val, 0 as Total_tickets
FROM TEMP_TICKET_STATE_month
where [Problem_Type_Name(Parent)]='Incident (Technical Issues)' and PRIORITY_TYPE_NAME in ('High') AND (ISNULL([Escalated to Closed time],0) <= 120 or ISNULL([Escalated to Resolved time],0) <=120)
group by [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)] ) t
group by t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)]) temp2
ON temp1.[Problem_Type_Name(Parent)]=temp2.[Problem_Type_Name(Parent)]
Group by temp1.[Assigned Tech],temp1.Close_Date,temp1.Job_ticket_id,temp1.NAME,temp1.[Problem_Type_Name(Parent)],temp1.[Problem_Type_Name(Child)] ,temp1.Report_Date
GO
答案 0 :(得分:2)
据我所知,只有一个分母可能为零。其他似乎是一个常数4。
用于捕获可怕的除以零的一种简单技术是 NullIf()。
例如NullIf(<any denominator>,0)
如果分母= 0
所以在你的情况下:
NullIf(cast(sum(t.Total_tickets) as decimal(38,2)),0)
答案 1 :(得分:1)
只有一个商可以失败 - 查询中有3个分区,2个除以常数。试试这个:
SELECT temp1.*,90 as Plan_val,max(cast(Round(temp2.actual,0) as int)) as actual_val FROM(
SELECT t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)] , REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id FROM (
SELECT 'NOC Incident Resolution - High' as [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)] , REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id
FROM TEMP_TICKET_STATE
UNION
SELECT 'NOC Incident Resolution - High' as [Problem_Type_Name(Parent)],[Problem_Type_Name(Child)] , REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id
FROM TEMP_TICKET_STATE
where [Problem_Type_Name(Parent)]='Incident (Technical Issues)' and PRIORITY_TYPE_NAME in ('High') AND (ISNULL([Escalated to Closed time],0) <= 120 or ISNULL([Escalated to Resolved time],0) <=120)) t
group by t.[Problem_Type_Name(Parent)] ,t.[Problem_Type_Name(Child)] , REPORT_DATE, CLOSE_DATE,[Assigned Tech],NAME , Job_ticket_id) temp1
INNER JOIN
(
SELECT t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)],0 AS plan_val,
-- CHANGE IS THE CASE ON THE NEXT LINE!
case when sum(t.Total_tickets) = 0 then null else cast(Round((cast(sum(t.Total_tickets) as decimal(38,2))- cast(sum(t.Alarm_Val) as decimal(38,2)))/cast(sum(t.Total_tickets) as decimal(38,2))*100,0) as int) end as actual FROM (
SELECT 'NOC Incident Resolution - High' as [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)],0 as Alarm_Val, cast (count(distinct [Job_ticket_id])/4 as int) as Total_tickets
FROM TEMP_TICKET_STATE_month
group by [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)]
UNION
SELECT 'NOC Incident Resolution - High' as [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)],cast (count(distinct [Job_ticket_id])/4 as int) as Alarm_Val, 0 as Total_tickets
FROM TEMP_TICKET_STATE_month
where [Problem_Type_Name(Parent)]='Incident (Technical Issues)' and PRIORITY_TYPE_NAME in ('High') AND (ISNULL([Escalated to Closed time],0) <= 120 or ISNULL([Escalated to Resolved time],0) <=120)
group by [Problem_Type_Name(Parent)] ,[Problem_Type_Name(Child)] ) t
group by t.[Problem_Type_Name(Parent)],t.[Problem_Type_Name(Child)]) temp2
ON temp1.[Problem_Type_Name(Parent)]=temp2.[Problem_Type_Name(Parent)]
Group by temp1.[Assigned Tech],temp1.Close_Date,temp1.Job_ticket_id,temp1.NAME,temp1.[Problem_Type_Name(Parent)],temp1.[Problem_Type_Name(Child)] ,temp1.Report_Date
GO