为了避免除以 0,我使用 CAST AS FLOAT 和 NULLIF。但由于某种原因,它给我的值是 0。我所做的只是划分
我做错了什么Declined
?Submissions
SELECT [Status Reason],
b.MonthNum,
b.YearNum,
ISNULL(SUM(CASE WHEN Status = 'Declined' THEN 1 ELSE 0 END ),0) as Declined,
(
SELECT COUNT(ControlNo)
FROM ClearanceReportMetrics
WHERE YEAR(EffectiveDate) =YEAR(GETDATE()) AND CompanyLine = 'Ironshore Insurance Company'
AND LineOfBusiness NOT IN ('SSP Commercial General Liability','SSP Property') AND Underwriter <> 'Batcheller, Jerry'
) AS Submissions,
/*And this is what gives 0's */
COALESCE(CAST(SUM(CASE WHEN Status = 'Declined' THEN 1 ELSE 0 END ) AS FLOAT) /
( /* Here I am using NULLIF to avoid division by 0 */
SELECT NULLIF(COUNT(ControlNo),0)
FROM ClearanceReportMetrics
WHERE EffectiveDate =YEAR(GETDATE()) AND CompanyLine = 'Ironshore Insurance Company'
AND LineOfBusiness NOT IN ('SSP Commercial General Liability','SSP Property') AND Underwriter <> 'Batcheller, Jerry'
),0) AS DeclinedRatio
FROM tblCalendar b
LEFT JOIN ClearanceReportMetrics a ON b.MonthNum = MONTH(a.EffectiveDate) and b.YearNum = YEAR(a.EffectiveDate)
--AND EffectiveDate >=DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE()))) AND EffectiveDate <= EOMONTH(GETDATE()) AND CompanyLine = 'Ironshore Insurance Company' AND Status = 'Declined'
--AND LineOfBusiness NOT IN ('SSP Commercial General Liability','SSP Property')
WHERE b.YearNum = YEAR(GETDATE())
GROUP BY b.YearNum,b.MonthNum,[Status Reason]
ORDER BY b.YearNum, b.MonthNum ,Declined DESC