mysql - How to combine several time spans -


lets say, have following table

| start               | end                 | activity +---------------------+---------------------+--------- | 2010-07-26 09:10:00 | 2010-07-26 09:21:00 | 6 | 2010-07-26 09:35:00 | 2010-07-26 09:47:00 | 5 | 2010-07-26 10:05:00 | 2010-07-26 10:45:00 | 5 | 2010-07-26 10:50:00 | 2010-07-26 11:22:00 | 6 | 2010-07-26 13:15:00 | 2010-07-26 13:43:00 | 7 | 2010-07-26 14:12:00 | 2010-07-26 14:55:00 | 2 

i want combine small time spans, getting average minutes activity per hour. that:

| start               | minutes_activity    | avg_activity +---------------------+---------------------+--------- | 2010-07-26 09:00:00 | 42                  | {avg value} | 2010-07-26 10:00:00 | 50                  | {avg value} | 2010-07-26 11:00:00 | 22                  | {avg value} | 2010-07-26 13:00:00 | 28                  | {avg value} | 2010-07-26 14:00:00 | 43                  | {avg value} 

note records can have activity minutes in 2 hours, i.e 10:50:00 - 11:22:00. in case, 10 minutes should added 10:00 , 22 minutes 11:00.

a solution, mysql, php or both appreciated.

thank advance.

let's assume table named records , time intervals don't span through more 2 hours:

select distinct(concat(date,' ',if(length(hour)=1, concat('0',hour), hour), ':00:00')) start,        sum(minutes) minutes_activity,        format(avg(activity),0) avg_activity (       select date, hour, minutes, activity       (             select date(start) date,                    hour(adddate(start, interval 1 hour)) hour,                    if(hour(start) < hour(end), minute(end), 0) minutes,                    activity activity             records             having minutes>0            ) t1       union (              select date(adddate(start, interval 1 day)) date,                     hour(adddate(start, interval 1 hour)) hour,                     if(hour(start) > hour(end), minute(end), 0) minutes,                     activity activity              records              having minutes>0                     , hour=0             )       union (              select date(start) date,                     hour(start) hour,                     if(hour(start) < hour(end),                        (60 - minute(start)),                        if(                           date(start) < date(end),                           (60 - minute(start)),                           (minute(end) - minute(start))                        )                     ) minutes,                     activity activity              records             )       order date asc, hour asc) t2 group concat(date,' ',if(length(hour)=1, concat('0',hour), hour), ':00:00'); 

... however, if have interval goes e.g. 2010-10-08 01:30:00 2010-10-08 03:13:00, (not so) simple sql query won't it.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -