SQL + Medium Query

Asked

Viewed 70 times

-1

Gentlemen, I have an appointment that makes the calculation by line.

The result is in the field calculo_diff.

Calculation is the difference DataHoraTerno,as shown below:

inserir a descrição da imagem aqui Now I need to make an average of calulo_diff, as described.

That is, it would be an average of:

Operando H. Norma

Waiting for Carreta

Someone can help me?

SELECT      Descricao
        ,   DataHoraTerno
        ,   @linha          := TIME_FORMAT(TIMEDIFF(@DataHoraTerno, DataHoraTerno), '%H%i') AS calculo_diff
        ,   @DataHoraTerno  := DataHoraTerno                                                AS valor_aux
        ,   @voltas         := @voltas + 1                                                  AS voltas
FROM        EmbarqueNavio
        ,   (
                SELECT  @DataHoraTerno  := 0
                    ,   @voltas         := 0
            ) AS vars
WHERE       NavioViagem = 3
        AND Terno       = 1
ORDER BY    DataHoraTerno DESC
  • 2

    Hello @Arita, Welcome to Sopt, and recommend you post the code instead of his image, so click [Edit], do not forget to do the [Tour]

2 answers

0

I’m guessing that’s what you want:

+---------------------+-----------------------+
| Descrição           | Média calculo_diff    |
+---------------------+-----------------------+
| Operando H. Norma   |   15                  |
+---------------------+-----------------------+
| Aguardando Carreta  |   20                  |
+---------------------+-----------------------+

If the field calculo_diff were it not a calculated field, it would be something like:

SELECT Descricao, AVERAGE(calculo_diff) as media_calculo_diff FROM tabelanome 
GROUP BY Descricao;

In your case tabelanome could be the result of the query you already make.

  • Thanks Juven_v I am layman but I understand your suggestion. So the diff calculation is generated by SQL now I do not know if it is worth it to do a routine so that record in the database this calculation.... I will have a hard time doing this. I mean every time the user registers a new schedule I will have to record the difference in the bank. ...

0


Try it this way:

SELECT      X.Descricao
        ,   AVG(X.calculo_diff) AS Media
FROM        (
                SELECT      Descricao
                        ,   DataHoraTerno
                        ,   @linha          := TIME_FORMAT(TIMEDIFF(@DataHoraTerno, DataHoraTerno), '%H%i') AS calculo_diff
                        ,   @DataHoraTerno  := DataHoraTerno                                                AS valor_aux
                        ,   @voltas         := @voltas + 1                                                  AS voltas
                FROM        EmbarqueNavio
                        ,   (
                                SELECT  @DataHoraTerno  := 0
                                    ,   @voltas         := 0
                            ) AS vars
                WHERE       NavioViagem = 3
                        AND Terno       = 1
                ORDER BY    DataHoraTerno DESC
            ) X
GROUP BY    X.Descricao
  • João Martins thank you so much for your attention !!! It worked perfectly !!!! !!!!

  • You’re welcome @Aritamarcelo! Mark the answer as correct and take advantage and give an UP!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.