[英]Select matching date to min() group by
I'm trying to select the lowest temperatures and it's related measure time grouped by weather station.我正在尝试选择最低温度,它是按气象站分组的相关测量时间。 It works fine, except that the measureTime column not matching the selected temperature.
它工作正常,只是 measureTime 列与所选温度不匹配。 Someone who can help me out?
谁能帮帮我?
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
Current result looks like this:当前结果如下所示:
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 |
Desired result would be:期望的结果是:
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 |
You should use a subquery for min temp and a join for retrieve the related measureTime您应该使用 min temp 的子查询和 join 来检索相关的 measureTime
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
Do not use GROUP BY
for a filtering query.不要将
GROUP BY
用于过滤查询。 A simple solution is a correlated subquery for filtering:一个简单的解决方案是用于过滤的相关子查询:
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;
You have to use aggregate functions before joining the 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.