简体   繁体   English

SQL查询SUM和除以不同日期计数

[英]SQL Query SUM and Divide by Distinct Date Count

I assistance, I am looking for the sum of a data field and then want to divide it by the number of distinct dates in that field. 我协助,我正在寻找数据字段的总和,然后想要除以该字段中不同日期的数量。

SUM(CASE WHEN dateResolved IS NOT NULL 
         THEN 1 ELSE 0 
    END) / DISTINCT(dateResolved) AvgPerDay

If there are 32 dates in dateResolved, with 5 distinct dates, I want it to return 6.4. 如果dateResolved中有32个日期,有5个不同的日期,我希望它返回6.4。

By default it does integer division you need : 默认情况下,它需要整数除法:

SUM(CASE WHEN dateResolved IS NOT NULL 
         THEN 1 ELSE 0 
    END) * 1.0 / COUNT(DISTINCT dateResolved) AvgPerDay

However simply count would also work : 然而,简单count也会起作用:

COUNT(dateResolved) * 1.0 / COUNT(DISTINCT dateResolved) AvgPerDay

COUNT(dateResolved) will ignore null values. COUNT(dateResolved)将忽略null值。

I would do this as: 我会这样做:

SUM(CASE WHEN dateResolved IS NOT NULL 
         THEN 1.0 ELSE 0 
    END) / COUNT(DISTINCT dateResolved) as AvgPerDay

But this is more simply phrased as: 但这更简单地表达为:

COUNT(dateResolved) * 1.0 / COUNT(DISTINCT dateResolved) as AvgPerDay

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM