简体   繁体   English

如何获得Microsoft SQL Server 2008中计数的平均值

[英]How do I get the average of a count in Microsoft SQL Server 2008

How can I get the average of this count? 如何获得该计数的平均值?

SELECT
    driver,
    COUNT(order_number) as month_total
FROM 
    Unloads
WHERE 
    completiondate BETWEEN '11-06-2017' AND '11-10-2017'
GROUP BY 
    driver
ORDER BY 
    month_total DESC;

This may seem like overkill, but data over a time series can have gaps, so to reliably average by month you need to ensure there are no gaps. 这似乎有些过分,但是一个时间序列上的数据可能会有差距,因此要可靠地按月平均,您需要确保没有差距。 A way (there are many) is to do something like the following so that the ranges exist as rows, then left join the data to those. 一种方法(有很多方法)是执行以下操作,以使范围以行的形式存在,然后将数据与这些行连接起来。 The other thing I particularly caution you on is to avoid using "between" for date ranges. 我特别提醒您的另一件事是避免对日期范围使用“介于”之间。 Use >= and < as you see below this ensures you don't accidentally over count for data sitting one the upper boundary dates. 如下面所见,使用>=<可以确保您不会意外地对上边界日期之一的数据进行计数。

declare @start_dt date;
set @start_dt = '20170601';

select
       u.driver, period_start_dt, period_end_dt
     , count(completiondate)            montly_count
     , AVG(COUNT(completiondate))OVER() avg_montly_count
from (
        select 
               dateadd(month,m.n,start_dt)   period_start_dt
             , dateadd(month,m.n+1,start_dt) period_end_dt
             , m.n                           month_no
        from (
               select @start_dt start_dt ) seed
        cross join (select 0 n union all
                    select 1 union all select 2 union all select 3 union all
                    select 4 union all select 5 union all select 6 union all
                    select 7 union all select 8 union all select 9 union all
                    select 10 union all select 11
                   ) m
     ) r

-- LEFT JOIN YOUR DATA
LEFT JOIN unloads u on u.completiondate >= r.period_start_dt and u.completiondate < r.period_end_dt

WHERE period_start_dt >= '20170601' and period_start_dt < '20171001'

group by
       u.driver , period_start_dt, period_end_dt      
order by 
       period_start_dt,  u.driver

Demo 演示版

If you want the average of the driver by day- you can subquery and divide the count for each driver by the number of days passed. 如果您想要按天计算驱动程序的平均值,则可以子查询并将每个驱动程序的计数除以经过的天数。 Number of days passed is the datediff of the two dates plus 1 day. 经过的天数是两个日期的datediff加1天。 It is plus 1 day since you have used BETWEEN, which is inclusive. 自从您使用BETWEEN(包括在内)以来已过了1天。 IOW, for days 6, 7, 8, 9, and 10- this is 5 days. IOW,第6、7、8、9和10天-这是5天。 But datediff(DAY, '2017-11-06', '2017-11-10') is only 4 days- so you must add +1 day to divide by the correct number of days. 但是datediff(DAY, '2017-11-06', '2017-11-10')只有4天-因此您必须添加+1天以除以正确的天数。 So the algorithm looks something like: 所以算法看起来像这样:

SELECT dT.driver
      ,month_total / (DATEDIFF(DAY, '2017-11-06', '2017-11-10') + 1) [Daily Driver Avg]

  FROM (
        SELECT
            driver,
            COUNT(order_number) as month_total
        FROM 
            Unloads
        WHERE 
            completiondate BETWEEN '2017-11-06' AND '2017-11-10'
        GROUP BY 
            driver
       ) AS dT

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

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