0

Tengo la siguiente consulta que trae la información de la temperatura y la humedad relativa por minuto de una zona en particular, el problema es que sólo puedo traer los datos del día de la siguiente manera:

SELECT plands.land AS Land, 
ROUND(AVG(meteorology.temperature),2)  AS Temperature, 
ROUND(AVG(meteorology.humidity),2)     AS Humidity,
WEEKOFYEAR(meteorology.date)           AS Week
FROM meteorology
INNER JOIN plands ON plands.id = meteorology.id_land
WHERE DATE(meteorology.date) BETWEEN '2016-09-12' AND '2016-09-23'
AND TIME(meteorology.date) >= '06:00:00'
AND TIME(meteorology.date) <= '18:00:00'
GROUP BY meteorology.id_land, WEEKOFYEAR(meteorology.date), YEAR(meteorology.date)
ORDER BY plands.land

lo que me da el siguiente resultado:

----------------------------------------------------
  Land    |    Temperature  |   Humidity  |  Week  |
----------------------------------------------------
 LAND1    |      16.13      |     92.03   |  37    |
----------------------------------------------------
 LAND1    |      16.46      |     84.35   |  38    | 
----------------------------------------------------  

Hasta aquí todo bien, hice la misma consulta cambiando en el WHERE las horas, pero no me funciona para traer los datos de las noche:

SELECT plands.land AS Land, 
    ROUND(AVG(meteorology.temperature),2)  AS Temperature, 
    ROUND(AVG(meteorology.humidity),2)     AS Humidity,
    WEEKOFYEAR(meteorology.date)           AS Week
    FROM meteorology
    INNER JOIN plands ON plands.id = meteorology.id_land
    WHERE DATE(meteorology.date) BETWEEN '2016-09-12' AND '2016-09-23'
    AND TIME(meteorology.date) < '06:00:00'
    AND TIME(meteorology.date) > '18:00:00'
    GROUP BY meteorology.id_land, WEEKOFYEAR(meteorology.date), YEAR(meteorology.date)
    ORDER BY plands.land

La consulta me llega vacía, lo que quiero es tratar de unir las consultas para que me de el siguiente resultado:

------------------------------------------------------------------------------
  Land | Temperatureday  | Humidityday |TemperatureNight | HumidityNigh |Week|
------------------------------------------------------------------------------
 LAND1 |   16.13         |   92.03     |     8.19        |     99.0     | 37 |
------------------------------------------------------------------------------
 LAND1 |   16.46         |   84.35     |     9.9         |     99.0     | 38 | 
------------------------------------------------------------------------------

Lo intente con un UNION de la siguiente manera:

SELECT plands.land AS Land, 
    ROUND(AVG(meteorology.temperature),2)  AS Temperatureday, 
    ROUND(AVG(meteorology.humidity),2)     AS Humidityday,
    WEEKOFYEAR(meteorology.date)           AS Week
    FROM meteorology
    INNER JOIN plands ON plands.id = meteorology.id_land
    WHERE DATE(meteorology.date) BETWEEN '2016-09-12' AND '2016-09-23'
    AND TIME(meteorology.date) >= '06:00:00'
    AND TIME(meteorology.date) <= '18:00:00'
    GROUP BY meteorology.id_land, WEEKOFYEAR(meteorology.date), YEAR(meteorology.date)
    ORDER BY plands.land
UNION
SELECT plands.land AS Land, 
        ROUND(AVG(meteorology.temperature),2)  AS Temperaturenight, 
        ROUND(AVG(meteorology.humidity),2)     AS Humiditynight,
        WEEKOFYEAR(meteorology.date)           AS Week
        FROM meteorology
        INNER JOIN plands ON plands.id = meteorology.id_land
        WHERE DATE(meteorology.date) BETWEEN '2016-09-12' AND '2016-09-23'
        AND TIME(meteorology.date) < '06:00:00'
        AND TIME(meteorology.date) > '18:00:00'
        GROUP BY meteorology.id_land, WEEKOFYEAR(meteorology.date), YEAR(meteorology.date)
        ORDER BY plands.land

El resultado que me da es el de la primera consulta y la segunda no da por el WHERE ya que no se porque me lo traer vacío.

Quisiera saber si estoy haciendo mal la consulta o si hay otra forma de hacerlo.

2 respuestas 2

1

Esta parte es la del problema, porqué la hora no puede ser menor a 6AM y mayor a 6PM al mismo tiempo

AND TIME(meteorology.date) < '06:00:00'
AND TIME(meteorology.date) > '18:00:00'

Cambiala por esta

AND (TIME(meteorology.date) < '06:00:00'
OR TIME(meteorology.date) > '18:00:00')
1
  • Muchas gracias por tu respuesta, me funciono por ese lado, pero quiero unir las dos consultas para obtener la temperatura y la humedad tanto del día como de la noche como lo explique en la pregunta! Commented el 23 sept. 2016 a las 15:19
0

Pude encontrar la solución de la siguiente manera:

SELECT Land,
       MIN(Temperatureday) AS Temperatureday,
       MIN(Humidityday) AS Humidityday,
       MIN(TemperatureNight) AS TemperatureNight,
       MIN(HumidityNight) AS HumidityNight,
       Week
FROM (
  SELECT plands.land AS Land,
     ROUND(AVG(meteorology.temperature),2) AS Temperatureday,
     ROUND(AVG(meteorology.humidity),2) AS Humidityday,
     NULL AS TemperatureNight,
     NULL AS HumidityNight,
     WEEKOFYEAR(meteorology.date) AS Week,
     YEAR(meteorology.date) AS Year
  FROM meteorology
  INNER JOIN plands ON plands.id = meteorology.id_land
  WHERE DATE(meteorology.date) BETWEEN '2016-09-12' AND '2016-09-23'
    AND TIME(meteorology.date) >= '06:00:00'
    AND TIME(meteorology.date) <= '18:00:00'
  GROUP BY meteorology.id_land,
           WEEKOFYEAR(meteorology.date),
           YEAR(meteorology.date)
  UNION ALL
  SELECT plands.land AS Land,
         NULL AS TemperatureNight,
         NULL AS HumidityNight,
         ROUND(AVG(meteorology.temperature),2) AS TemperatureNight,
         ROUND(AVG(meteorology.humidity),2) AS HumidityNight,
         WEEKOFYEAR(meteorology.date) AS Week,
         YEAR(meteorology.date) AS Year
  FROM meteorology
  INNER JOIN plands ON plands.id = meteorology.id_land
  WHERE DATE(meteorology.date) BETWEEN '2016-09-12' AND '2016-09-23'
    AND (TIME(meteorology.date) < '06:00:00'
         OR TIME(meteorology.date) > '18:00:00')
  GROUP BY meteorology.id_land,
           WEEKOFYEAR(meteorology.date),
           YEAR(meteorology.date)
) AS itsownalias
GROUP BY Land, Week, Year

Uní las dos consultas, tanto del día como la noche y las uní con un UNION ALL dentro del FROM y con un alias en el SELECT los pude llamar.

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.