如何修改此查询以添加包含原始记录总数的字段最大值的新字段?

时间:2017-09-19 13:17:55

标签: mysql sql database rdbms

我不是那么进入数据库,我在实现查询时遇到以下问题。我正在使用 MySql

我有一个像这样的MeteoForecast表:

CREATE TABLE MeteoForecast (
  id                   BigInt(20) NOT NULL AUTO_INCREMENT,
  localization_id      BigInt(20) NOT NULL,
  seasonal_forecast_id BigInt(20),
  meteo_warning_id     BigInt(20),
  start_date           DateTime NOT NULL,
  end_date             DateTime NOT NULL,
  min_temp             Float,
  max_temp             Float,
  icon_link            VarChar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, 
  PRIMARY KEY (
      id
  )
) ENGINE=InnoDB AUTO_INCREMENT=3 ROW_FORMAT=DYNAMIC DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;

它包含meteo预测信息,如下所示:

id                   localization_id      start_date              end_date                min_temp             max_temp             icon_link                                                                                                                                                                                                                                                      
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1                    1                    18/09/2017 06:00:00     18/09/2017 12:00:00     15                   24                   Mostly_Cloudy_Icon.png                                                                                                                                                                                                                                         
2                    1                    18/09/2017 12:00:00     18/09/2017 18:00:00     15                   24                   Light_Rain.png                                                                                                                                                                                                                                                 
3                    1                    19/09/2017 06:00:00     19/09/2017 12:00:00     12                   22                   Mostly_Cloudy_Icon.png                                                                                                                                                                                                                                         
4                    1                    19/09/2017 12:00:00     19/09/2017 18:00:00     13                   16                   Mostly_Cloudy_Icon.png                                                                                                                                                                                                                                         
5                    1                    20/09/2017 06:00:00     20/09/2017 12:00:00     18                   26                   Light_Rain.png                                                                                                                                                                                                                                                 
6                    1                    20/09/2017 12:00:00     20/09/2017 18:00:00     17                   25                   Light_Rain.png                  

因此,正如您在上一个数据集中所看到的,每条记录都有一个起始日期时间和结束日期时间。这是因为我在特定的一天收集了更多的预测信息(它基于时间范围,在每天的例子中,记录从早上06:00到12:00,另一条记录从12:00到18:00)

所以,我创建了这个简单的查询,它提取了特定范围内的所有记录(在这种情况下为2天):

select * from MeteoForecast 
where start_date between  '2017-09-18 06:00:00' and '2017-09-20 06:00:00'
order by start_date desc;

我必须按以下方式修改此查询:

对于上一个查询检索到的每个记录,必须添加一个名为 global_max_temp 的新字段,该字段是同一天中 max_temp 字段的最大值。

做一个与 start_date 值等于 19/09/2017 ... 相关的记录相关的示例,这些是我需要获取的记录:

id                   localization_id      start_date              end_date                min_temp             max_temp             icon_link                       global_max_temp                                                                                                                                                                                                                                                      
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    
3                    1                    19/09/2017 06:00:00     19/09/2017 12:00:00     12                   22                   Mostly_Cloudy_Icon.png          22                                                                                                                                                                                                                                     
4                    1                    19/09/2017 12:00:00     19/09/2017 18:00:00     13                   16                   Mostly_Cloudy_Icon.png          22

正如您在此处所看到的,最后一个字段(在此模拟中手动插入)是 global_max_temp ,并且在与当天相关的两个记录中都包含值 22 ,因为它是与特定日期相关的所有记录的 max_temp 字段的最大值。

这是计算这些 global_max_temp 值的查询:

select max(max_temp) from MeteoForecast 
where start_date = '2017-09-19 06:00:00'

如何将此功能添加到原始查询中?

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情:

SELECT A.*, B.GLOBAL_MAX_TEMP
FROM (
    select id, start_date, end_date, min_temp, max_temp
    from MeteoForecast 
    where start_date between  '2017-09-18 06:00:00' and '2017-09-20 06:00:00'
    ) A
INNER JOIN (SELECT  date(start_date) AS date_only, MAX(max_temp) AS GLOBAL_MAX_TEMP
            FROM MeteoForecast
            WHERE start_date BETWEEN  '2017-09-18 06:00:00' and '2017-09-20 06:00:00'
            GROUP BY date(start_date)
            ) B ON date(A.start_date) = B.date_only
ORDER by start_date desc;