Mysql
 sql >> Database >  >> RDS >> Mysql

Come si crea una differenza di tempo media tra i record in MySQL?

Devi convertire il tuo timestamp in un numero per poterlo arrotondare. E quando hai il timestamp medio, riconvertilo in una data.

select device, count(*) as cnt, FROM_UNIXTIME(AVG(UNIX_TIMESTAMP(time)), '%i:%s')
from database.table
group by device
having cnt>1
order by device;

Nota:se consideri i tempi negativi, la tua media è ovvia! In caso contrario, è meglio calcolarlo con un linguaggio di scripting (php, c#, ruby...)

average := avg(ts1–ts2, ts2-ts3, ts3-ts4)
= (ts1–ts2 + ts2-ts3 + ts3-ts4)/3
= (ts4-ts1)/3

Generalizzazione:

average = (last entry - first entry) / (number of entries - 1)