MySQL average number of hours between created datetimes for a specific time interval

心已入冬 提交于 2019-12-13 03:43:50

问题


I have a table with a field called "created" which is a datetime field.

Assume NOW() is midnight on 2013-07-21, so NOW() = 2013-07-21 23:59:59

Now let's say I query for all records WHERE created BETWEEN DATE_SUB(NOW(), INTERVAL 4 DAYS) AND NOW()

Let's say that returns results like 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 to add the start and end datetime for the interval I used (4 days) to that result set, so now I have:

  1. 2013-07-18 00:00:00 (4 days ago from 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 now I want a query that will give me the average amount of hours between those 6 datetime results.

That is (8 + 24 + 24 + 24 + 24 + 16) / 6 which is an average of 20 hours.

I found this on stack over flow

SELECT
TIME_TO_SEC(TIMEDIFF(end,start)) AS timediff
FROM
Sessions
GROUP BY
DATE(start)

The problem with this query is that I'd have to run it 6 times passing in the dates each time (in a PHP loop) and then add all the results and / 6 and then / by 3600.

How can get the result I want just using mySQL?

Thanks!

Just for a little further clarification:

Assume two things.

  1. A user is going to select a date range (in this case July 21st to July 18th)

  2. Other users are using a different service that generates that created record each time they use the service.

The first user wants to know how often on average (in hours) that the second used that second service between the selected date range.

So, it needs to account for the time between 2013-07-18 00:00:00 and 2013-07-18 08:00:00 (those 8 hours matter) and so do the 16 hours at the end, because the user did not use the service during those time periods.

Basically I don't only want the average amount of hours between the 4 initial created records, which I think (correct me if i'm wrong) that is what Gordon suggested.


回答1:


The average is the difference between the first and last records divided by the count plus some number.

SELECT (UNIX_TIMESTAMP(max(date(created)+1), min(date(created)))/1000)/(count(*)+2) AS timediffsecs
FROM Sessions
WHERE created BETWEEN DATE_SUB(NOW(), INTERVAL 4 DAYS) AND NOW()

What is this doing? First, it is not adding additional records to the data. Instead, it is just rounding down the earlier date and rounding up the later date. UNIX_TIMETAMP produces values in milliseconds since some point in time. Take the difference between the biggest and smallest. Finally, divide by the number of rows encountered plus 2 (I think it should be the count plus 1, but your question says count plus 2).




回答2:


I finally figured out what was causing my confusion. I had a used a formula similar to what Gordon suggested and got 19.2, but my calculation came up as 20, so I didn't think the formula worked, however I just did the math wrong.

There are only FIVE intervals between my SIX dates, so it's 8+24+24+24+16 / 5 which is 19.2, here is the query I ended up using.

I hope this helps someone.

SELECT TIMESTAMPDIFF(SECOND,DATE_SUB(NOW(), INTERVAL 4 DAY),NOW()) / (COUNT(created) - 1) / 3600 AS time_diff_hours, member_id
FROM members
WHERE created BETWEEN DATE_SUB(NOW(), INTERVAL 4 DAY) AND NOW()
GROUP BY members.id


来源:https://stackoverflow.com/questions/17779155/mysql-average-number-of-hours-between-created-datetimes-for-a-specific-time-inte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!