【发布时间】:2016-06-28 22:10:15
【问题描述】:
我正在根据每年的年份和月份计算比率。两个 CASE 语句都带来了正确的值,需要进行划分。但由于某种原因,我得到了 0 或 -100
;WITH cte_yoyComparison
AS
(
SELECT Year(EffectiveDate) AS EffectiveYear,
Month(EffectiveDate) AS EffectiveMonth,
SUM(Premium) as Premium
FROM Test_Plaza_ProductionReport
WHERE YEAR(EffectiveDate) <> 2017
GROUP BY Year(EffectiveDate),
Month(EffectiveDate)
)
SELECT *, b.YearNum,b.MonthNum,
((case when b.YearNum=2016 and b.MonthNum=1 THEN Premium end )) /(case when b.YearNum = 2015 and b.MonthNum = 1 THEN Premium end) as Ratio,
((((case when b.YearNum=2015 and b.MonthNum=1 THEN Premium else 0 end)) /NULLIF((case when b.YearNum = 2016 and b.MonthNum = 1 THEN Premium else 0 end),0))-1)*100 as Ratio1,
case when b.YearNum=2015 and b.MonthNum=1 THEN Premium else 0 end as CorrectValue,
case when b.YearNum = 2016 and b.MonthNum = 1 THEN Premium else 0 end as CorrectValue1
FROM tblCalendar b
LEFT JOIN cte_yoyComparison a ON b.MonthNum=a.EffectiveMonth AND b.YearNum=a.EffectiveYear
WHERE b.YearNum<>2017
Order BY EffectiveYear desc,EffectiveMonth
【问题讨论】:
-
Premium列的数据类型是什么? -
Int 除以较大的 int 将产生零。将分子或分母转换为带小数的东西
-
我尝试将分子转换为 FLOAT,结果相同