mysql 时间平均值_MySQL:获取时间差异的平均值?

bd96500e110b49cbb3cd949968f18be7.png

I have a table called Sessions with two datetime columns: start and end.

For each day (YYYY-MM-DD) there can be many different start and end times (HH:ii:ss). I need to find a daily average of all the differences between these start and end times.

An example of a few rows would be:

start: 2010-04-10 12:30:00 end: 2010-04-10 12:30:50

start: 2010-04-10 13:20:00 end: 2010-04-10 13:21:00

start: 2010-04-10 14:10:00 end: 2010-04-10 14:15:00

start: 2010-04-10 15:45:00 end: 2010-04-10 15:45:05

start: 2010-05-10 09:12:00 end: 2010-05-10 09:13:12

...

The time differences (in seconds) for 2010-04-10 would be:

50

60

300

5

The average for 2010-04-10 would be 103.75 seconds. I would like my query to return something like:

day: 2010-04-10 ave: 103.75

day: 2010-05-10 ave: 72

...

I can get the time difference grouped by start date but I'm not sure how to get the average. I tried using the AVG function but I think it only works directly on column values (rather than the result of another aggregate function).

This is what I have:

SELECT

TIME_TO_SEC(TIMEDIFF(end,start)) AS timediff

FROM

Sessions

GROUP BY

DATE(start)

Is there a way to get the average of timediff for each start date group? I'm new to aggregate functions so maybe I'm misunderstanding something. If you know of an alternate solution please share.

I could always do it ad hoc and compute the average manually in PHP but I'm wondering if there's a way to do it in MySQL so I can avoid running a bunch of loops.

Thanks.

解决方案SELECT DATE(start) AS startdate, AVG(TIME_TO_SEC(TIMEDIFF(end,start))) AS timediff

FROM Sessions

GROUP BY

startdate

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值