我有一个烦人的问题,就是阻止我生成一些数据; SQL作业总共有23个步骤,在21日失败。
-- Step 21 Create the table z1QReportOverview
-- Create z1QReportProjectOverview.sql
-- Project Overview - By Category (Part 4).sql
USE database
SELECT z1QReportProjectOverview1.[ERA Category] AS Category,
z1QReportProjectOverview1.[Total Projects Signed],
z1QReportProjectOverview1.[Total Spend Under Review],
z1QReportProjectOverview1.[Avg. Project Size],
z1QReportProjectOverview2.[Work in Progress],
z1QReportProjectOverview2.[Implemented],
z1QReportProjectOverview2.[No Savings],
z1QReportProjectOverview2.[Lost],
CONVERT(decimal(18,0),[Lost])/CONVERT(decimal(18,0),[Total Projects Signed]) AS [Loss Ratio],
z1QReportProjectOverview2.[Completed],
(
CONVERT(decimal(18,0),([Completed]+[Implemented]))/
CONVERT(decimal(18,0),([Completed]+[Implemented]+[Lost]))
)
AS [Success Ratio],
z1QReportProjectOverview3.[Avg. Spend] AS [Average Spend],
z1QReportProjectOverview3.[Avg. Savings] AS [Average Savings],
z1QReportProjectOverview3.[Avg. Savings %] AS [Average Savings %]
INTO dbo.z1QReportProjectOverview
FROM dbo.z1QReportProjectOverview1
JOIN dbo.z1QReportProjectOverview2
ON (z1QReportProjectOverview1.[ERA Category] = z1QReportProjectOverview2.[ERA Category])
JOIN dbo.z1QReportProjectOverview3
ON (z1QReportProjectOverview2.[ERA Category] = z1QReportProjectOverview3.[ERA Category])
ORDER BY Category
我相信我知道造成零差错的原因。
'Lost'字段由三个字段组成,在某些情况下(非常罕见),所有3个字段都为0,导致'Lost'字段中的0。
我相信这是错误的主要原因,但是那里还有第二个分区,我在SQL上非常垃圾,因此我的问题是:
我应该在哪里放置CASE WHEN条款?
- 最有可能写错了这个:( “CASE当[Lost] = 0 SET [Total Projects Signed] = 0”
时非常感谢任何建议!
答案 0 :(得分:3)
使用NULLIF
来处理除以零错误
........
Isnull(CONVERT(DECIMAL(18, 0), [Lost]) / NULLIF(CONVERT(DECIMAL(18, 0), [Total Projects Signed]), 0), 0) AS [Loss Ratio],
Isnull(CONVERT(DECIMAL(18, 0), ( [Completed] + [Implemented] )) /
NULLIF(CONVERT(DECIMAL(18, 0), ( [Completed] + [Implemented] + [Lost] )), 0), 0) AS [Success Ratio],
........
答案 1 :(得分:2)
您可以使用CASE
检查分割值是否为0
。
CASE WHEN CONVERT(decimal(18,0),[Lost]) <> 0 THEN
CONVERT(decimal(18,0),[Lost])/CONVERT(decimal(18,0),[Total Projects Signed])
ELSE 0 END AS [Loss Ratio],
z1QReportProjectOverview2.[Completed],
CASE WHEN CONVERT(decimal(18,0),([Completed]+[Implemented]+[Lost])) <> 0 THEN
(CONVERT(decimal(18,0),([Completed]+[Implemented]))/CONVERT(decimal(18,0),([Completed]+[Implemented]+[Lost])))
ELSE 0 END AS [Success Ratio],
答案 2 :(得分:2)
你可以这样使用CASE WHEN
:
CASE
-- check if either value is 0 (which will cause error)
WHEN CONVERT(decimal(18,0),[Lost]) = 0
OR CONVERT(decimal(18,0),[Total Projects Signed]) = 0
-- if so, return 0
THEN 0
-- otherwise perform calculation
ELSE CONVERT(decimal(18,0),[Lost])/CONVERT(decimal(18,0),[Total Projects Signed])
END AS [Loss Ratio]
答案 3 :(得分:0)
@Tanner:
所以布置这样的代码应该可以正常工作吗?
CONVERT(decimal(18,0),[Lost])/CONVERT(decimal(18,0),[Total Projects Signed]) AS [Loss Ratio],
CASE
-- check if either value is 0 (which will cause error)
WHEN CONVERT(decimal(18,0),[Lost]) = 0
OR CONVERT(decimal(18,0),[Total Projects Signed]) = 0
-- if so, return 0
THEN 0
-- otherwise perform calculation
ELSE CONVERT(decimal(18,0),[Lost])/CONVERT(decimal(18,0),[Total Projects Signed])
END AS [Loss Ratio]
z1QReportProjectOverview2.[Completed],
(
CONVERT(decimal(18,0),([Completed]+[Implemented]))/
CONVERT(decimal(18,0),([Completed]+[Implemented]+[Lost]))
)
AS [Success Ratio],