【问题标题】:MySQL: Calculate average post for every hour in each dayMySQL:计算每天每小时的平均帖子
【发布时间】:2013-09-30 19:42:40
【问题描述】:

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

【问题讨论】:

    标签: mysql


    【解决方案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
    

    【讨论】:

    • 感谢您的回复。抱歉,我不明白你所说的分割是什么意思?
    • 按月、日、小时。您基本上是将分组的行“拆分”为这些较小的组
    • 我在查询“post.datePost BETWEEN '2010-05-01' AND '2010-05-31'”上方做了一些更改,我对 avg 函数有一些问题。我更新了上面的问题。谢谢
    • 你正在做 1 个值的平均值,thecount,返回值的总和/值的计数,在你的情况下是 the_count/1。你想平均什么?另外,在...之间更改 post.datepost 到 date(post.datepost) 之间。否则,您将丢失间隔中的最后一天,因为您的日期被转换为格式为“2010-05-31 00:00:00”的日期时间
    • 另外请将您的第一个查询编辑为当前表单,当前查询和 putput 没有相同的列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2017-12-23
    • 1970-01-01
    • 2018-11-29
    • 2015-04-11
    相关资源
    最近更新 更多