0

tengo una base de datos que absorve datos por minuto, meidante la siguiente query que me obtengo la media horaria de datos validos a la hora, el tema es que para un dato sea válido tiene que tener mas del 75% de datos validos a la hora, y en base a ese 75% de datos validos por hora calcular medias diarias y horarias válidas, es decir, tengo que calcular la media diaria y mensual en base a la media horaria válida, ¿Cómo podría hacerlo? Gracias. Así calculo la media horaria:

   SELECT q.Hora, SUM(q.PARTICULAS) AS PARTICULAS, SUM(q.PRESION) AS PRESION, sum(q.PRESIONDIF) AS 
   PRESIONDIF,
   SUM(Q.TEMPERATURASONDA) AS TEMPERATURASONDA, SUM(Q.TEMPERATURA) AS TEMPERATURA, SUM(Q.CAUDAL) AS 
   CAUDAL
   FROM (
   SELECT DATEPART(hour, Fecha) as Hora, CONVERT(INT, AVG(particulas)) as PARTICULAS, 0 as PRESION, 0 
   as PRESIONDIF, 0 AS TEMPERATURASONDA, 0 AS TEMPERATURA,
   0 AS CAUDAL
   FROM filtro1
   WHERE flag1 ='1'  GROUP BY DATEPART(hour, Fecha)  having count(flag1)>45
   UNION
   SELECT DATEPART(hour, Fecha) as Hora, 0 AS PARTICULAS, AVG(presion) AS PRESION, 0 as PRESIONDIF, 0 
   AS TEMPERATURASONDA, 0 AS TEMPERATURA,
   0 AS CAUDAL
   FROM filtro1
   WHERE flag2 ='0'  GROUP BY DATEPART(hour, Fecha) having count(flag2)>45
   UNION
   SELECT DATEPART(hour, Fecha) as Hora, 0 AS PARTICULAS, 0 AS presion, AVG(PRESIONDIF), 0 AS 
   TEMPERATURASONDA, 0 AS TEMPERATURA,
   0 AS CAUDAL
   FROM filtro1
   WHERE flag3 ='0'  GROUP BY DATEPART(hour, Fecha) having count(flag3)>45
   UNION
   SELECT DATEPART(hour, Fecha) as Hora, 0 AS PARTICULAS, 0 AS presion, 0 AS PRESIONDIF, 
   AVG(TEMPERATURASONDA), 0 AS TEMPERATURA,
   0 AS CAUDAL
   FROM filtro1
   WHERE flag4 ='0'  GROUP BY DATEPART(hour, Fecha) having count(flag4)>45
   UNION
   SELECT DATEPART(hour, Fecha) as Hora, 0 AS PARTICULAS, 0 AS presion, 0 AS PRESIONDIF, 0 AS 
   TEMPERATURASONDA, AVG(TEMPERATURA),
   0 AS CAUDAL
   FROM filtro1
   WHERE flag5 ='0'  GROUP BY DATEPART(hour, Fecha) having count(flag5)>45
   UNION
   SELECT DATEPART(hour, Fecha) as Hora, 0 AS PARTICULAS, 0 AS presion, 0 AS PRESIONDIF, 0 AS 
   TEMPERATURASONDA, 0 AS TEMPERATURA,
   AVG(CAUDAL)
   FROM filtro1
   WHERE flag6 ='0'  GROUP BY DATEPART(hour, Fecha) having count(flag6)>45
   ) q
   GROUP BY q.Hora
   ORDER BY q.Hora

Este es el resultado que me gustaría obtener:

introducir la descripción de la imagen aquí

2
  • Tienes muchas preguntas relacionadas con el mismo tema. Creo que necesitas mostrar el problema completo o simplemente buscar a un mentor de tiempo completo. Commented el 18 mar. 2020 a las 14:40
  • Gracias, encontré ya el método creando una tabla CTE
    – mourazo
    Commented el 18 mar. 2020 a las 14:43

1 respuesta 1

1

Sin datos de prueba, es complicado entender lo que quieres hacer, pero seguramente será más sencillo obtener la respuesta si simplificamos un poco tu consulta para llamar la tabla una sola vez.

WITH ctePromedios AS(
    SELECT DATEPART(hour, Fecha) as Hora,
           CONVERT(INT, AVG(CASE WHEN flag = '1' THEN particulas END)) as PARTICULAS,
           COUNT(CASE WHEN flag = '1' THEN 1 END) as PARTICULAS_COUNT,
           AVG(CASE WHEN flag2 ='0' THEN presion END) as PRESION,
           COUNT(CASE WHEN flag2 ='0' THEN 1 END) as PRESION_COUNT,
           AVG(CASE WHEN flag3 ='0' THEN presiondif END) as PRESIONDIF,
           COUNT(CASE WHEN flag3 ='0' THEN 1 END) as PRESIONDIF_COUNT,
           AVG(CASE WHEN flag4 ='0' THEN TEMPERATURASONDA END) as TEMPERATURASONDA,
           COUNT(CASE WHEN flag4 ='0' THEN 1 END) as TEMPERATURASONDA_COUNT,
           AVG(CASE WHEN flag5 ='0' THEN TEMPERATURA END) as TEMPERATURA,
           COUNT(CASE WHEN flag5 ='0' THEN 1 END) as TEMPERATURA_COUNT,
           AVG(CASE WHEN flag6 ='0' THEN CAUDAL END) as CAUDAL,
           COUNT(CASE WHEN flag6 ='0' THEN 1 END) as CAUDAL_COUNT
    FROM filtro1
    GROUP BY DATEPART(hour, Fecha) 
)
SELECT Hora,
        CASE WHEN PARTICULAS_COUNT > 45         THEN PARTICULAS       ELSE 0 END AS PARTICULAS      ,
        CASE WHEN PRESION_COUNT > 45            THEN PRESION          ELSE 0 END AS PRESION         ,
        CASE WHEN PRESIONDIF_COUNT > 45         THEN PRESIONDIF       ELSE 0 END AS PRESIONDIF      ,
        CASE WHEN TEMPERATURASONDA_COUNT > 45   THEN TEMPERATURASONDA ELSE 0 END AS TEMPERATURASONDA,
        CASE WHEN TEMPERATURA_COUNT > 45        THEN TEMPERATURA      ELSE 0 END AS TEMPERATURA     ,
        CASE WHEN CAUDAL_COUNT > 45             THEN CAUDAL           ELSE 0 END AS CAUDAL          
FROM ctePromedios

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.