我需要帮助,我正在寻找一个数据字段的总和,然后将其除以该字段中不同日期的数量。
declarations
如果dateResolved中有32个日期,有5个不同的日期,我希望它返回6.4。
答案 0 :(得分:3)
默认情况下,它会进行整数除法:
SUM(CASE WHEN dateResolved IS NOT NULL
THEN 1 ELSE 0
END) * 1.0 / COUNT(DISTINCT dateResolved) AvgPerDay
不过count
也可以工作:
COUNT(dateResolved) * 1.0 / COUNT(DISTINCT dateResolved) AvgPerDay
COUNT(dateResolved)
将忽略null
值。
答案 1 :(得分:0)
我会这样:
SUM(CASE WHEN dateResolved IS NOT NULL
THEN 1.0 ELSE 0
END) / COUNT(DISTINCT dateResolved) as AvgPerDay
但这更简单地表达为:
COUNT(dateResolved) * 1.0 / COUNT(DISTINCT dateResolved) as AvgPerDay