mysql - 基于mysql中的周数求平均值

标签 mysql

我正在尝试完成一个查询,该查询需要两个指标的平均值:入站调用和未接调用。但我从来没有如此细致地处理过星期几和每小时的数据,所以我不确定我的总计是否正确,更不用说得到正确的平均值了。

基本上,从 2018 年 1 月 1 日开始,我想要周一至周五上午 7 点到下午 6 点每小时的平均来电和未接来电。我们关闭了 7 天,我得到了 48 行,所以这就是我的预期。

如果过去 3 个周一是这样的:

creationtimestamp   | Legtype1  |  answered  

07/23/18 08:15:00   |     2     |     0             
07/23/18 08:25:00   |     2     |     1           
07/23/18 08:35:00   |     2     |     1               
07/30/18 08:15:00   |     2     |     0           
07/30/18 08:25:00   |     2     |     0           
07/30/18 08:35:00   |     2     |     0           
07/30/18 08:45:00   |     2     |     1           
07/30/18 08:55:00   |     2     |     0           
08/06/18 08:15:00   |     2     |     0           
08/06/18 08:25:00   |     2     |     1           
08/06/18 08:35:00   |     2     |     0           
08/06/18 08:45:00   |     2     |     0  

周一上午 8 点到 9 点期间总共接到 12 个电话,其中 4 个未接。如果我从 8 点到 9 点查询这三个星期一,我会期望:

Monday | 8 | 4 | 1.3

但是我不知道如何计算每个工作日的所有调用的总和,将它们相加并除以该工作日的数量?我下面的查询目前有“SUM”而不是平均值,但我不确定如何获取我需要的平均值,因为它取决于每个工作日的数字。

SELECT 
    dayname(s.creationtimestamp) as day, -- weekdays
    HOUR(s.creationtimestamp) as Hour, -- Hours
    sum(case when legtype1 = 2 then 1 else 0 end) as total_calls, -- total inbound
    sum(case when  legtype1 = 2 and answered = 0 then 1 else 0 end)as total_missed

FROM session s
WHERE (s.creationtimestamp >= '2018-01-01' AND creationtimestamp < now())
and WEEKDAY(s.creationtimestamp) BETWEEN 0 AND 4 -- Monday through friday
AND HOUR(s.creationtimestamp) between  7 and 18 -- 7am to 6pm
GROUP BY dayname(s.creationtimestamp),  HOUR(s.creationtimestamp)
order by dayofweek(s.creationtimestamp), hour asc;

重申一下:查询有效,但我不确定是否根据从今年第一天到现在的每个工作日和小时 block 正确聚合。

这是一个 fiddle : http://sqlfiddle.com/#!9/7b6b72

最佳答案

我认为一切对你来说都很好,只是多一点,希望下面的查询对你有帮助

select day,Hour,Avg(total_calls) as avg_total_calls,
Avg(total_missed) as avg_total_missed from
(
SELECT 
    dayname(s.creationtimestamp) as day, -- weekdays
    HOUR(s.creationtimestamp) as Hour, -- Hours
    sum(case when legtype1 = 2 then 1 else 0 end) as total_calls, -- total inbound
    sum(case when  legtype1 = 2 and answered = 0 then 1 else 0 end)as total_missed

FROM session s
WHERE (s.creationtimestamp >= '2018-01-01' AND creationtimestamp < now())
and WEEKDAY(s.creationtimestamp) BETWEEN 0 AND 4 -- Monday through friday
AND HOUR(s.creationtimestamp) between  7 and 18 -- 7am to 6pm
GROUP BY dayname(s.creationtimestamp),  HOUR(s.creationtimestamp)

) as T group by day,Hour 
order by day,Hour asc

关于mysql - 基于mysql中的周数求平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51766880/

相关文章:

mysql - 获取同一列的总计,但每个 SELECT 别名都包含一个特定的字符串

c# - 如何在table1的指定列之后将table2中的列连接到table1?

php - 将时间转换为 MySQL 时间

c++ - 构建缺少错误 mysqlcppconn 的 C++ 脚本(没有这样的文件)

当列中的值互换时,MySQL 对 2 列进行分组

mysql - JPA一对多删除并将外键设置为null

MySQL "create trigger tr_emp before insert or update"

ruby-on-rails - MySql Query::当查询被触发时,如何解决数据中的 's 问题

mysql - 如何将当前周过滤为日期范围

MySQL - 计算每组中的行数