Calculating average time

4

In my database I have a table that has the fields entrada , saida , the two fields have the format D-MM-YYYY H:M:S , I would like to do an average calculation.

Ex: The average wait time is 30 min

Based on all the results of my table, is it possible? I thought of some ways to do this, but I could not even come up with a solution or a functional logic.

I use Mysql . id | id_ent | entrada | saida | status

    
asked by anonymous 04.09.2017 / 14:52

2 answers

2

In addition you can calculate the average directly in your query which returns the database data as follows:

SELECT AVG(TIME_TO_SEC(TIMEDIFF(t.saida, t.entrada))) / 60 AS espera_media
  FROM tabela t
    
04.09.2017 / 15:19
5

In Javascript (and in most languages, actually), dates and times are represented in memory as the amount of milliseconds since an arbitrary date-time. You can see this number by using the getTime function of type Date, like this:

var agora = new Date();
agora.getTime();

So, to find out how long a call took, you can do the following:

var horaEntrada = foo;
var horaSaida = bar;

var tempoAtendimentoEmMilissegundos = bar.getTime() - foo.getTime();

Just change foo and bar to the actual in and out times.

If you want to convert the service time to other units of measure, it is easy:

var tempoAtendimentoEmSegundos = tempoAtendimentoEmMilissegundos / 1000;
var tempoAtendimentoEmMinutos = tempoAtendimentoEmSegundos / 60;

// etc., etc.

Finally, to get the average, just make a simple account. Accumulate all times in one sum, and divide the sum by the number of calls.

var soma = 0;
for (let i = 0; i < tempos.length; i++) {
    soma += tempos[i];
}
var media = soma / tempos.length;
    
04.09.2017 / 15:01