MySQL记录平均工作日和每小时

时间:2018-08-08 16:37:59

标签: mysql sql

我目前正在按拨打电话的小时和星期几进行分组:

SELECT HOUR(creationtimestamp), dayname(creationtimestamp), count(*)
FROM cdrdb.session
WHERE DATE_SUB(creationtimestamp,INTERVAL 1 HOUR) And 
creationtimestamp > DATE_SUB(NOW(), INTERVAL 1 DAY)
GROUP BY HOUR(creationtimestamp);

哪个返回:

0   Monday  23
0   Tuesday 96
0   Wednesday   85
0   Thursday    76
0   Friday  114
0   Saturday    47
0   Sunday  32
1   Monday  2
1   Tuesday 20
1   Wednesday   19
1   Thursday    16
1   Friday  31
1   Saturday    9
1   Sunday  6
2   Monday  13
2   Tuesday 18
2   Wednesday   5
2   Thursday    5
2   Friday  8

这一直持续到23小时,但这并不是我想要得到的。

说完之后,我想获得大约60个结果,平均记录时间为上午7点至下午6点 因此,在星期一至星期五的每个小时,我都会有一个行,其中包含该时间范围内的平均记录数。

也许看起来更像

Monday | 7:00 | 89
Monday | 8:00 | 53
Monday | 9:00 | 53
...
Tuesday | 8:00 | 53

该如何更改分组

更新查询:

正确的小时数和天数,但平均值可能不正确。尝试获取每个星期一上午8到859的平均通话量,等等。

SELECT 
dayname(creationtimestamp) as day, 
HOUR(creationtimestamp) as Hour, 
(sum(callsIN/ 60) * 100 as averageCalls,
(sum(callsMissed)/sum(callsIN) * 100 as averageMissedCalls
  FROM session s
    WHERE (creationtimestamp >= '2018-01-01' AND creationtimestamp < now())
  and WEEKDAY(creationtimestamp) BETWEEN 0 AND 4
  AND HOUR(creationtimestamp) between  7 and 18 
  AND finallycalledpartyno IN ( 7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)
  GROUP BY dayname(creationtimestamp),  HOUR(creationtimestamp)
  order by dayofweek(creationtimestamp), hour asc;

1 个答案:

答案 0 :(得分:2)

您可以按天名(creationtimestamp),HOUR(creationtimestamp)分组,并将结果限制为60

  SELECT HOUR(creationtimestamp), dayname(creationtimestamp), count(*)
  FROM cdrdb.session
  WHERE DATE_SUB(creationtimestamp,INTERVAL 1 HOUR) And 
  creationtimestamp > DATE_SUB(NOW(), INTERVAL 1 DAY)
  AND HOUR(creationtimestamp) between  7 and 18 
  GROUP BY dayname(creationtimestamp),  HOUR(creationtimestamp) 
  ORDER BY dayname(creationtimestamp),  HOUR(creationtimestamp)  limit 60;