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:

  1. 2013-07-18 08:00:00
  2. 2013-07-19 08:00:00
  3. 2013-07-20 08:00:00
  4. 2013-07-21 08:00:00

i want add start , end datetime interval used (4 days) result set, have:

  1. 2013-07-18 00:00:00 (4 days ago now())
  2. 2013-07-18 08:00:00
  3. 2013-07-19 08:00:00
  4. 2013-07-20 08:00:00
  5. 2013-07-21 08:00:00
  6. 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.

  1. a user going select date range (in case july 21st july 18th)

  2. 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

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -