Det ville være nyttigt, hvis du forberedte eksempeltabel og dataindsættelser.
Læs dette link for at lære, hvorfor det er så vigtigt, hvis du vil have hjælp :http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html
Denne gang har jeg lavet det til dig, klik på dette link: http://sqlfiddle.com/#!2/9719a/2
Og prøv denne forespørgsel (du finder denne forespørgsel sammen med eksempeldata under ovenstående link) :
select alias1.*,
timestampdiff( second, previous_viewed_at, viewed_at )
as time_between_viewings
from (
select alias.*,
(
select viewed_at from (
select
( select count(*) from asset_usages y
where x.asset_id = y.asset_id
and y.viewed_at < x.viewed_at
) as rn,
x.*
from asset_usages x
) xyz
where xyz.asset_id = alias.asset_id
and xyz.rn = alias.rn - 1
) previous_viewed_at
from (
select
( select count(*) from asset_usages y
where x.asset_id = y.asset_id
and y.viewed_at < x.viewed_at
) as rn,
x.*
from asset_usages x
) alias
) alias1;