简体   繁体   中英

How can I modify this query to add a new field containing the maximum value of a field of a subset of the total original records?

I am not so into database and I have the following problem implementing a query. I am using MySql

I have a MeteoForecast table like this:

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;

It contains meteo forecast information, something like this:

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                  

So, as you can see in the previous dataset, each record have a starting datetime and and ending datetime. This because I am collecting more forecast information in a specific day (it is based on time range, in the example for each day a record from 06:00 am to 12:00 and another record from 12:00 to 18:00 pm).

So, I created this simple query that extracts all the records in a specific range (in this case 2 days):

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;

I have to modify this query in the following way:

For each record retrieved by the previous query have to be added a new field named global_max_temp that is the maximum value of the max_temp field in the same day.

Doing an example related to the records related to theday having start_date value equal to 19/09/2017... , these are the records that I need to obtain:

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

As you can see here the last field (inserted manually in this mock) is global_max_temp and in both records related to this day contains the value 22 because it is the maximum value of the max_temp field of all the records related to a specific day.

This is the query calculating these global_max_temp value:

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

How can I add this feature to my original query?

Can you try something like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM