我正在尝试将计算值(条件总和)除以另一个计算值(总计数)。结果总是输出为0.我已经仔细检查了原始数据,并且预计不会发生这种情况。以下是当前状态的查询:
SELECT
BUCKET,
SUM(CASE WHEN RANDOM_ACTUAL>=RANDOM_BENCHMARK_LOW THEN 1 ELSE 0 END)/COUNT(*) AS CALCULATION
FROM DATA_TEAM_COLD_CALL_RESULTS
GROUP BY BUCKET
我也尝试过使用IFF代替case并从子查询中查询。查询正在Periscope中从SQL Server 2012数据库运行。提前谢谢!
答案 0 :(得分:2)
你得到整数除法,需要添加小数位以强制小数结果,最容易乘以1.0:
SELECT BUCKET
, SUM(CASE WHEN RANDOM_ACTUAL>=RANDOM_BENCHMARK_LOW THEN 1 ELSE 0 END *1.0)/COUNT(*) AS CALCULATION
FROM DHCSource.dbo.DATA_TEAM_COLD_CALL_RESULTS
GROUP BY BUCKET