1

我试图计算每天每小时发布的平均帖子,我必须这样做 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'           #
4

1 回答 1

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
于 2013-09-29T16:09:42.253 回答