MySQL查询生成穿孔卡dataviz

时间:2014-12-16 15:09:09

标签: mysql datetime dayofweek hour

我有一个包含聊天记录的数据库,其中包含一个datime字段,该消息的作者和消息。

我不想做一个返回表格的查询,该表格将工作日与一天中的小时数相关联,计算发送的邮件数量,使用与github生成穿孔卡图表相同的格式:

[day, hour, number of messages]

[
[0,0,23],
[0,1,20],
[0,2,56],
[]...]

我尝试过这条SQL指令,但我觉得它不起作用。

select 
    dayofweek(date_time) as DAY, 
    hour(date_time) as `HOUR`, 
    sum(dayofweek(date_time)) as Msgs 
from chat
group by DAY(date_time)
order by `DAY`

我得到的回报是:

[[1,1,4734]
[1,20,4503]
[1,11,6510]
[1,0,7058]
[2,0,6518]
[2,8,3913]
[2,9,3885]
[2,0,2305]
[2,9,4471]
[2,0,3703]
[3,0,3315]
[3,0,9600]
[3,0,3910]
[3,9,4956]
[3,0,2692]
[4,0,5225]
[4,10,4815]
[4,0,7667]
[4,11,5249]
[4,0,5121]
[5,8,3148]
[5,10,4947]
[5,0,4109]
[5,0,4775]
[6,0,4970]
[6,0,4342]
[6,12,4488]
[6,0,7191]
[7,0,3790]
[7,9,9294]
[7,9,4749]]

我不知道发生了什么,但返回应该有168行,7 * 24,我在这里缺少什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

问题是你只按天分组,而不是白天和小时。而不是

select 
    dayofweek(date_time) as DAY, 
    hour(date_time) as `HOUR`, 
    sum(dayofweek(date_time)) as Msgs 
from chat
group by DAY(date_time)
order by `DAY`

select 
    dayofweek(date_time) as DAY, 
    hour(date_time) as `HOUR`, 
    sum(dayofweek(date_time)) as Msgs 
from chat
group by dayofweek(date_time), hour(date_time)
order by `DAY`,HOUR