Skip to main content
etiquetas editadas
Enlace
Fabian Sierra
  • 1k
  • 1
  • 15
  • 24
Origen Enlace
Fabian Sierra
  • 1k
  • 1
  • 15
  • 24

Consulta para traer datos del día y la noche sql

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.