获取错误:消息8134,级别16,状态1,行1

时间:2014-10-30 19:35:48

标签: sql-server tsql divide-by-zero

我已经回复了对此问题的其他回复,但无法找到对我的情况的适当回应。我试图划分"实际时间"按"估计小时数"。但是,估计的小时数可以为空(无条目)或为零。实际小时数可以为零。因此,当我进入分部声明时,我无法估计小时为零或实际小时为零。我的"合并"声明似乎没有帮助。任何建议都会有所帮助。

SELECT PUV.ProjectName, PUV.[Project ID], PUV.[Project Director], PUV.[Project Owner (Manager)], PUV.[Project Type1], **COALESCE( PUV.[Estimated Hours], 0 ) AS EstHours
,PUV.ProjectStatusDate, PUV.ProjectStartDate, PUV.ProjectBaseline0StartDate, PUV.ProjectActualStartDate, PUV.ProjectStartVariance, PUV.ProjectBaseline0FinishDate
,PUV.ProjectFinishDate, PUV.ProjectFinishVariance, PUV.ProjectActualFinishDate, PUV.ProjectWork, PUV.ProjectBaseline0Work, PUV.ProjectActualWork, PUV.ProjectWorkVariance, PUV.ProjectRemainingWork
,PUV.[Project Phase], PUV.[Hold - Canceled Indicator], CWI.StageName, TB.TB_BASE_NUM, MAX (TB.CREATED_DATE) RecentBaseline
,CASE WHEN PUV.[Estimated Hours] is null then 0 END AS [Estimated%]
,Case When PUV.ProjectActualWork <> 0 THEN cast(COALESCE(PUV.ProjectActualWork,0) as DECIMAL(20,2) )/Cast(COALESCE(PUV.[Estimated Hours],0)as decimal(20,2)) Else  0 End   AS  [Estimated%]* 
--DATEDIFF(day, PUV.ProjectBaseline0FinishDate, PUV.ProjectFinishDate)
FROM  MSP_EpmProject_UserView AS PUV
LEFT OUTER JOIN (
    SELECT WSI.ProjectUID, WP.PhaseName, WP.PhaseUID, WS.StageName, WS.StageUID
    FROM  MSP_EpmWorkflowStage  AS WS              
    INNER JOIN MSP_EpmWorkflowPhase AS WP ON  WS.PhaseUID = WP.PhaseUID            
    INNER JOIN MSP_EpmWorkflowStatusInformation AS WSI ON WS.StageUID = WSI.StageUID AND WSI.StageEntryDate IS NOT NULL AND (WSI.StageStatus != 0 AND WSI.StageStatus != 4)
    ) AS CWI ON PUV.ProjectUID = CWI.ProjectUID
JOIN sps_ppm_IT_Published.dbo.MSP_TASK_BASELINES AS TB ON PUV.ProjectUID = TB.PROJ_UID
WHERE TB.TB_BASE_NUM = 0
GROUP BY PUV.ProjectName, PUV.[Project ID], PUV.[Project Director], PUV.[Project Owner (Manager)], PUV.[Project Type1], PUV.[Estimated Hours]
,PUV.ProjectStatusDate, PUV.ProjectStartDate, PUV.ProjectBaseline0StartDate, PUV.ProjectActualStartDate, PUV.ProjectStartVariance, PUV.ProjectBaseline0FinishDate
,PUV.ProjectFinishDate, PUV.ProjectFinishVariance, PUV.ProjectActualFinishDate, PUV.ProjectWork, PUV.ProjectBaseline0Work, PUV.ProjectActualWork, PUV.ProjectWorkVariance, PUV.ProjectRemainingWork
,PUV.[Project Phase], PUV.[Hold - Canceled Indicator], CWI.StageName, TB.TB_BASE_NUM
ORDER BY PUV.ProjectName

1 个答案:

答案 0 :(得分:0)

您的标题与问题完全无关,从您的数据库发布超长的别名查询并不是获取帮助的好方法。

幸运的是,我很好奇地看到“msg-8134-level-16-state-1-line-1”是否是来自火星的秘密消息,我检查了它。

无论如何,这里有一个小例子,说明如何使用CASE语句来避免被零除:

SELECT
        CASE WHEN example.estimated_hours != 0 AND example.estimated_hours IS NOT NULL
        THEN 100 / example.estimated_hours
        ELSE 0 END AS ratio
FROM (
        SELECT estimated_hours 
        FROM (
                VALUES (0), (1), (NULL), (2), (3)) 
                AS mock_data (estimated_hours))
        AS example;

您可以将其复制/粘贴到SQL终端并运行它以查看输出:

ratio 
-------
     0
   100
     0
    50
    33
(5 rows)