具有平均子查询的SQL查询

时间:2011-03-14 16:47:37

标签: mysql sql

我正在对天气数据库进行SQL查询,我需要wind_direction和windspeed 这是我目前的查询:

SELECT wind_direction,
       windspeed
  FROM weather
  WHERE time >= curdate() and
        time < (curdate() + interval 1 day) AND
        windspeed > 0
  ORDER BY wind_direction ASC

这将删除windspeed = 0的所有值,并且仅显示今天的数据。

查询输出:

wind_direction   windspeed  
0               10.1  
0               11.2  
23              7.6  
23              1.4  

正如您所看到的,我得到的重复值是可以理解的,但我的图形系统不支持这一点,它不知道要使用哪个值。
我需要的是一个独特的wind_direction和avg()为这个方向的风速。

1 个答案:

答案 0 :(得分:5)

SELECT  wind_direction, AVG(windspeed)
FROM    weather
WHERE   time >= curdate() and time < (curdate() + interval 1 day)
        AND windspeed > 0
GROUP BY
        wind_direction
ORDER BY
        wind_direction ASC

如果你想要一个具有最高平均速度的wind_direction,请使用:{/ p>

SELECT  wind_direction, AVG(windspeed) AS avg_speed
FROM    weather
WHERE   time >= curdate() and time < (curdate() + interval 1 day)
        AND windspeed > 0
GROUP BY
        wind_direction
ORDER BY
        avg_speed DESC 
LIMIT 1