我的代码似乎无法使用[Actual Closed Loans]
变量并将其除以[Loan Count]
变量。
我尝试在[Actual Closing Ratio]
变量中执行此步骤。但是我得到零结果。即使我在这段代码中添加了一个新变量,并且1 / count(*)
SQL似乎也不喜欢它。
我知道我必须在此上下文中错误地使用Count(*)
。但我无法理解。
我的查询:
Select
[PORT_DATE],
Count (*) as [Loan Count], --this works fine
sum(Case when [Closed or Fallen Out] ='c' then 1 else 0 end) as [Actual Closed Loans],--this works fine
sum(Case when [Closed or Fallen Out] ='C' then 1 else 0 end)/count(*) as [Actual Closing Ratio], --although code works doesnt produce correct result, output for this variable is all zeros
From
dbo.XYZ
Group by
[PORT_DATE]
order by
[PORT_DATE]
答案 0 :(得分:3)
这是由于整数除法,将乘法加1.0
得到数值结果。
sum(Case when [Closed or Fallen Out] ='C' then 1 else 0 end)*1.0/count(*)