我试图计算每天每小时的平均帖子,我必须这样做113个月。 Post表中有此属性timePosted,DatePosted和Text。我还需要加入两个表格帖子和帖子,因为我只想获得类别ID 3。
到目前为止,这是我已经完成的查询。
select datePost as daytime,
HOUR(timePost) as thehour,
count(TEXT) as thecount
from post, thread
where date(post.datePost) BETWEEN '2010-05-01' AND '2010-05-31'
and post.threadID = thread.threadID
and thread.CatID = 3
group by datePost, thehour
上面的子查询将此返回给我:
daytime thehour thecount
'2010-05-01', '0', '3'
'2010-05-01', '1', '16'
'2010-05-01', '2', '2'
'2010-05-01', '4', '1'
'2010-05-01', '7', '1'
我尝试做平均但是问题是它返回了与数字相同的数字。示例thecount为3,然后Avg返回3.00000
所以我试图得到这个结果:
daytime thehour thecount Avg
'2010-05-01', '0', '3' #
'2010-05-01', '1', '16' #
'2010-05-01', '2', '2' #
'2010-05-01', '4', '1' #
'2010-05-01', '7', '1' #
答案 0 :(得分:1)
按照
分割所需的任何内容进行分组select month(timePost), day(timePost), hour(timePost),avg(the_count)
from
(
select datePost as the_day,
timePost,
count(TEXT) as the_count
from post, thread
where post.datePost = '2010-05-03'
and post.threadID = thread.threadID
and thread.CatID = 3
group by the_day,the_hour
) s
group by 1,2,3
以文字的形式获取星期几,请使用
case dayofweek(date) when 1 then 'sunday' when 2 then 'monday' .... end as dayofweek
额外的百分比
select datePost as daytime,
HOUR(timePost) as thehour,
count(TEXT) as thecount,
count(TEXT)/thecount_daily as percent_this_hour
from post
inner join thread on post.threadID = thread.threadID
inner join ( select datePost as daytime_daily,
count(TEXT) as thecount_daily
from post inner join thread on post.threadID = thread.threadID
where date(post.datePost) BETWEEN '2010-05-01' AND '2010-05-31'
and thread.CatID = 3
group by datePost)daily on daily.daytime_daily=datepost
where date(post.datePost) BETWEEN '2010-05-01' AND '2010-05-31'
and thread.CatID = 3
group by datePost, thehour
表示该时间范围内的平均每小时数,
select datePost as daytime,
HOUR(timePost) as thehour,
count(TEXT) as thecount,
hourly_average
from post
inner join thread on post.threadID = thread.threadID
inner join ( select hour(timepost) as daytime_daily,
count(TEXT)/count(distinct datePost) as hourly_average
from post inner join thread on post.threadID = thread.threadID
where date(post.datePost) BETWEEN '2010-05-01' AND '2010-05-31'
and thread.CatID = 3
group by datePost)daily on daily.daytime_daily=hour(timepost)
where date(post.datePost) BETWEEN '2010-05-01' AND '2010-05-31'
and thread.CatID = 3
group by datePost, thehour