简体   繁体   中英

Split months into 4 and retrieve the average value per day

This is the output of a query on a temporary table created in a complex stored procedure in MySQL :

在此处输入图片说明

The goal now is to have the average duration per day like we have here, but for each week by month

So I want to take every month and divide it into 4 set of days with the average duration per day .
Therefore no matter how many days there are in the month, I will have 4 values for it.

How can I do that ?

Note : If it is easier I can do it with php, since I will use the data with this language.

You have a query that groups per day, eg:

select 
  the_date as day,
  sec_to_time(avg(timestampdiff(second, start_time, end_time))) as duration
from ...
group by the_date;

You want days 1-7, 8-14, 15-21, 22-end per month instead. Use CASE WHEN to build the groups.

select 
  year(the_date) as year,
  month(the_date) as month,
  case 
    when day(the_date) <=  7 then '01-07'
    when day(the_date) <= 14 then '08-14'
    when day(the_date) <= 21 then '15-21'
    else                          '22-end'
  end as day_range,
  sec_to_time(avg(timestampdiff(second, start_time, end_time))) as duration
from ...
group by year, month, day_range
order by year, month, day_range;

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