Average between select results

Asked

Viewed 59 times

1

I need to calculate the average repair time for each application. Knowing that I have in hand the repair code, application code and time for each repair, what would be the best way to perform this calculation in SQL?

SELECT 
    OS.TAG AS OS,
    APLIC.TAG AS APLICACAO,
    CONVERT(DECIMAL(10,2), ROUND(DATEDIFF(SECOND, OS.MAQPAR, OS.MAQFUN)/3600.0, 2)) AS TEMPO_REPARO
FROM [ENGEMAN].[APLIC] AS APLIC
    JOIN [ENGEMAN].[ORDSERV] AS OS ON APLIC.CODAPL = OS.CODAPL
WHERE OS.STATORD != 'C'
    AND OS.MAQPAR IS NOT NULL 
    AND OS.CODFIL = 2 

inserir a descrição da imagem aqui

NOTE: SQL SERVER 2012!

  • I believe that the function of aggregation avg(tempo_reparo) along with a GROUP BY aplicacao can help you.

2 answers

0

   SELECT DISTINCT 
        OS.TAG AS OS,
        APLIC.TAG AS APLICACAO,
        AVG(CONVERT(DECIMAL(10,2), ROUND(DATEDIFF(SECOND, OS.MAQPAR, OS.MAQFUN)/3600.0, 2)) OVER(PARTITION BY OS.TAG,APLIC.TAG ) AS MEDIA_TEMPO_REPARO
    FROM [ENGEMAN].[APLIC] AS APLIC
        JOIN [ENGEMAN].[ORDSERV] AS OS ON APLIC.CODAPL = OS.CODAPL
    WHERE OS.STATORD != 'C'
        AND OS.MAQPAR IS NOT NULL 
        AND OS.CODFIL = 2 

0

You can use the function AVG in the column to calculate the average:

SELECT 
    avg(CONVERT(DECIMAL(10,2), ROUND(DATEDIFF(SECOND, OS.MAQPAR, OS.MAQFUN)/3600.0, 2))) AS TEMPO_REPARO
FROM [ENGEMAN].[APLIC] AS APLIC
    JOIN [ENGEMAN].[ORDSERV] AS OS ON APLIC.CODAPL = OS.CODAPL
WHERE OS.STATORD != 'C'
    AND OS.MAQPAR IS NOT NULL 
    AND OS.CODFIL = 2 
  • This function only repeated the field TEMPO_REPARO.

  • Can pass the create of the table and the Inserts for me to test here?

Browser other questions tagged

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