【问题标题】:SQL LIMIT dynamic NSQL LIMIT 动态 N
【发布时间】:2019-06-19 10:36:41
【问题描述】:

我想从具有指定县编号的所有站点获取(最后一行)平均空气温度。

因此,我的解决方案类似于

SELECT AVG(air_temperature) 
  FROM weather 
 WHERE station_id IN (
       SELECT station_id 
         FROM stations 
        WHERE county_number = 25
       )
 ORDER 
    BY id DESC 
 LIMIT 1; 

显然,这并没有给出正确的行,因为它返回了基于曾经记录的一个站点的所有 air_temperature 的平均 air_temperature。

回到问题,我想从具有指定县编号的每个站点获取最后插入行的平均空气温度。

表格天气

+------------------+-------------+------+-----+---------+----------------+
| Field            | Type        | Null | Key | Default | Extra          |
+------------------+-------------+------+-----+---------+----------------+
| id               | int(11)     | NO   | PRI | NULL    | auto_increment |
| station_id       | char(20)    | YES  | MUL | NULL    |                |
| timestamp        | timestamp   | YES  |     | NULL    |                |
| air_temperature  | float       | YES  |     | NULL    |                |
+------------------+-------------+------+-----+---------+----------------+

台站

+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| station_id    | char(20)    | NO   | PRI | NULL    |       |
| county_number | int(10)     | YES  |     | NULL    |       |
+---------------+-------------+------+-----+---------+-------+

表格被最小化

【问题讨论】:

    标签: mysql sql group-by aggregate-functions


    【解决方案1】:

    我建议使用 join 和一些过滤来执行此操作:

    select avg(w.air_temperature)
    from weather w join
         stations s
         on w.station_id = s.station_id
    where s.county_number = 25 and
          w.timestamp = (select max(w2.timestamp) from weather w2 where w2.station_id = w.station_id)
    

    【讨论】:

      【解决方案2】:

      您可以通过检查max(timestamp)获取最后插入的行:

      SELECT 
        AVG(w.air_temperature) 
      FROM weather w 
      INNER JOIN (
        SELECT station_id, max(timestamp) maxtimestamp FROM weather GROUP BY station_id        
      ) t
      ON w.station_id = t.station_id AND w.timestamp = t.maxtimestamp
      WHERE 
        w.station_id IN (SELECT station_id FROM stations WHERE county_number = 25)
      

      【讨论】:

        【解决方案3】:

        更新:我刚刚注意到您的 timestamp 列可以为空,并且您正在谈论“最后插入的行”。那是ID最大的那个。因此:

        从 MySQL 8 开始,您可以使用窗口函数来读取表一次:

        select avg(air_temperature) 
        from
        (
          select air_temperature, id, max(id) over (partition by station_id) as max_id
          from weather 
          where station_id in (select station_id from stations where county_number = 25)
        ) analyzed
        where id = max_id;
        

        在旧版本中,您必须阅读该表两次:

        select avg(air_temperature) 
        from weather 
        where (station_id, id) in
        (
          select station_id, max(id)
          from weather 
          where station_id in (select station_id from stations where county_number = 25)
          group by station_id
        );
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-21
          • 2018-12-19
          • 1970-01-01
          • 2022-01-02
          相关资源
          最近更新 更多