J'essaie de sélectionner les températures les plus basses et le temps de mesure associé regroupé par station météo. Cela fonctionne bien, sauf que la colonne measureTime ne correspond pas à la température sélectionnée. Quelqu'un qui peut m'aider?
name airTemp measureTime ---------------|---------|---------------------| Latnivaara A | 7.5 | 2019-07-27 03:00:00 | Nikkaluokta A | 8.6 | 2019-07-27 03:00:00 | Graninge | 8.9 | 2019-07-27 04:20:01 | Rensjön A | 8.9 | 2019-07-27 04:00:00 | Pajala A | 9.4 | 2019-07-27 03:00:00 | à kroken | 9.4 | 2019-07-27 05:20:02 | Norrhög | 9.6 | 2019-07-27 00:20:02 | Karesuando | 9.8 | 2019-07-27 03:00:00 | Noppikoski | 9.8 | 2019-07-27 01:20:00 | Nikkaluokta | 9.8 | 2019-07-27 02:00:00 |
Le résultat actuel ressemble à ceci:
name airTemp measureTime ---------------|---------|---------------------| Latnivaara A | 7.5 | 2019-07-27 00:00:00 | Nikkaluokta A | 8.6 | 2019-07-27 00:00:00 | Graninge | 8.9 | 2019-07-27 00:20:02 | Rensjön A | 8.9 | 2019-07-27 00:00:00 | Pajala A | 9.4 | 2019-07-27 00:00:00 | à kroken | 9.4 | 2019-07-27 00:20:02 | Norrhög | 9.6 | 2019-07-27 00:20:02 | Karesuando | 9.8 | 2019-07-27 00:20:02 | Noppikoski | 9.8 | 2019-07-27 00:20:01 | Nikkaluokta | 9.8 | 2019-07-27 00:20:00 |
Le résultat souhaité serait:
SELECT
weatherstations.weatherstation_name AS name,
min(weatherstations_data.weather_airtemp) AS airTemp,
weatherstations_data.weather_measuretime AS measureTime
FROM
weatherstations
LEFT JOIN
weatherstations_data
ON
weatherstations.weatherstation_id = weatherstations_data.weatherstation_id
WHERE
weatherstations_data.weather_airtemp IS NOT NULL
GROUP BY
weatherstations.weatherstation_name
ORDER BY
airTemp ASC
LIMIT
10
3 Réponses :
Vous devez utiliser une sous-requête pour min temp et une jointure pour récupérer le measureTime associé
select t.name, t.airTemp, d.measureTime
from (
SELECT
s.weatherstation_id
s.weatherstation_name AS name,
min(d.weather_airtemp) AS airTemp
FROM weatherstations s
LEFT JOIN weatherstations_data d ON s.weatherstation_id = d.weatherstation_id
WHERE d.weather_airtemp IS NOT NULL
GROUP BY s.weatherstation_name
ORDER BY airTemp ASC
LIMIT 10
) t
inner join weatherstations_data d ON t.weatherstation_id = d.weatherstation_id
AND t.airTemp = d.weather_airtemp
N'utilisez pas GROUP BY pour une requête de filtrage. Une solution simple est une sous-requête corrélée pour le filtrage:
SELECT ws.weatherstation_name AS name,
ws.weather_airtemp AS airTemp,
wsd.weather_measuretime AS measureTime
FROM weatherstations ws LEFT JOIN
weatherstations_data wsd
ON ws.weatherstation_id = wsd.weatherstation_id
WHERE wsd.weather_airtemp IS NOT NULL AND
wsd.weather_airtemp = (SELECT MIN(wsd2.weather_airtemp)
FROM weatherstations_data wsd2
WHERE wsd2.weatherstation_id = wsd.weatherstation_id
)
ORDER BY airTemp ASC
LIMIT 10;
Vous devez utiliser des fonctions d'agrégation avant de rejoindre les tables.
SELECT
t1.weatherstation_name AS name,
t2.weather_airtemp AS airTemp,
t2.weather_measuretime AS measureTime
FROM
weatherstations t1
LEFT JOIN(
SELECT weatherstation_id , weather_measuretime , min(weather_airtemp) as weather_airtemp
FROM weatherstations_data
group by weather_airtemp
) t2
ON
t1.weatherstation_id = t2.weatherstation_id
WHERE
t2.weather_airtemp IS NOT NULL
GROUP BY
t1.weatherstation_name
LIMIT
10
Un jeu de résultats sans jeu de données est comme un bâton sans sucette. Pas très amusant.
Double possible de Obtenir des enregistrements avec une valeur maximale pour chaque groupe de résultats SQL groupés
Il s'agit d'un problème courant dans SQL connu sous le nom de «groupwise-maximum». Trouvez une description du problème et plusieurs solutions dans le manuel: dev.mysql.com/doc/refman/8.0/en/...