MySQL average number of hours between created datetimes for a specific time interval -
i have table field called "created" datetime field.
assume now() midnight on 2013-07-21, now() = 2013-07-21 23:59:59
now let's query records created between date_sub(now(), interval 4 days) , now()
let's returns results this:
- 2013-07-18 08:00:00
- 2013-07-19 08:00:00
- 2013-07-20 08:00:00
- 2013-07-21 08:00:00
i want add start , end datetime interval used (4 days) result set, have:
- 2013-07-18 00:00:00 (4 days ago now())
- 2013-07-18 08:00:00
- 2013-07-19 08:00:00
- 2013-07-20 08:00:00
- 2013-07-21 08:00:00
- 2013-07-21 23:59:59 (now())
and want query give me average amount of hours between 6 datetime results.
that (8 + 24 + 24 + 24 + 24 + 16) / 6 average of 20 hours.
i found on stack on flow
select time_to_sec(timediff(end,start)) timediff sessions group date(start)
the problem query i'd have run 6 times passing in dates each time (in php loop) , add results , / 6 , / 3600.
how can result want using mysql?
thanks!
just little further clarification:
assume 2 things.
a user going select date range (in case july 21st july 18th)
other users using different service generates created record each time use service.
the first user wants know how on average (in hours) second used second service between selected date range.
so, needs account time between 2013-07-18 00:00:00 , 2013-07-18 08:00:00 (those 8 hours matter) , 16 hours @ end, because user did not use service during time periods.
basically don't want average amount of hours between 4 initial created records, think (correct me if i'm wrong) gordon suggested.
the average difference between first , last records divided count plus number.
select (unix_timestamp(max(date(created)+1), min(date(created)))/1000)/(count(*)+2) timediffsecs sessions created between date_sub(now(), interval 4 days) , now()
what doing? first, not adding additional records data. instead, rounding down earlier date , rounding later date. unix_timetamp
produces values in milliseconds since point in time. take difference between biggest , smallest. finally, divide number of rows encountered plus 2 (i think should count plus 1, question says count plus 2).
Comments
Post a Comment