简体   繁体   中英

MySQL: Calculate average post for every hour in each day

I trying to calculate the average post made for every hour for each day and I have to do this for 113 months. Inside the Post table have this attribute timePosted, DatePosted and Text. I also need to join two table post and thread because I only want to get category id number 3.

So far this is query that I have done.

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

The above subquery return me this:

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'

I try to do the avg but the issue is that it return me the same number as thecount. Example thecount is 3 then the Avg return me 3.00000

So I trying to get this outcome:

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'           #

just group by whatever you need to split by

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

to get day of week as text, use

case dayofweek(date) when 1 then 'sunday' when 2 then 'monday' .... end as dayofweek

for the extra percent do

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

for average per hour in that timeframe,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM