select avg(unix_timestamp(v1.sold_ts)-unix_timestamp(v2.sold_ts)) as avg_diff from vending_log v1, vending_log v2 where v2.sold_ts = (select sold_ts from vending_log where sold_ts < v1.sold_ts order by 1 desc limit 1 );
]]>
select avg(vv1.diff) from (select *,unix_timestamp(v1.sold_ts) - unix_timestamp((select sold_ts from vending_log where sold_ts < v1 .sold_ts order by sold_ts desc limit 1)) as diff from vending_log v1) vv1;
select avg(vv1.diff) from
(select *,unix_timestamp(v1.sold_ts) -
unix_timestamp((select max(sold_ts)
from vending_log
where sold_ts < v1 .sold_ts)) as diff
from vending_log v1) vv1;
The next one is fast, but relies on user variables.
set @a:=null;
set @b:=null;
select avg(vv1.diff) from
(select @b:=@a,@a:=sold_ts, unix_timestamp(sold_ts) - unix_timestamp(@b) as diff
from vending_log order by sold_ts) vv1;