Skip to main content
1 de 2
mourazo
  • 1.2k
  • 2
  • 11
  • 24

Calcular media diaria en base a media por horas SQLSERVER

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
mourazo
  • 1.2k
  • 2
  • 11
  • 24