SQL Count(*)不适用于具有分组依据的公式

时间:2014-11-26 18:26:05

标签: sql sql-server count group-by average

我的代码似乎无法使用[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]

1 个答案:

答案 0 :(得分:3)

这是由于整数除法,将乘法加1.0得到数值结果。

sum(Case when [Closed or Fallen Out] ='C' then 1 else 0 end)*1.0/count(*)