简体   繁体   English

如何修改此查询以添加一个新字段,其中包含原始记录总数的子集的某个字段的最大值?

[英]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 我正在使用MySql

I have a MeteoForecast table like this: 我有一个像这样的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;

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). 这是因为我将在特定日期收集更多的预测信息(它基于时间范围,在示例中,每天的记录是从06:00 am到12:00,另一条记录是12:00到18:00 pm) 。

So, I created this simple query that extracts all the records in a specific range (in this case 2 days): 因此,我创建了一个简单的查询,该查询提取了特定范围内的所有记录(在本例中为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;

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. 对于前一个查询检索到的每个记录,必须添加一个名为global_max_temp的新字段,该字段是同一天max_temp字段的最大值。

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: 做一个与与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

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. 如您所见,最后一个字段(在此模拟中手动插入)是global_max_temp,并且与该天相关的两条记录中的值都为22,因为它是与特定日期相关的所有记录中max_temp字段的最大值。

This is the query calculating these global_max_temp value: 这是计算以下global_max_temp值的查询:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改此查询以返回多个记录,以返回包含原始记录串联的单个记录? - How can I change this query returning multiples records to return a single record containing the concatenation of the original records? 检索包含字段最大值的整行 - Retrieving the entire row containing maximum value of a field 如何添加一个字段来计算查询检索到的记录数? 为什么我会收到此错误消息? - How can I add a field that count the number of the records retrieved by my query? Why am I obtaining this error message? 如何修改此PHP查询以添加数据库值“时间戳”? - How can I modify this PHP Query to add the database value 'timestamp'? 如何使每个新记录的字段默认值为一个值? - How can I make a field default a value with every new record? 如何将自定义记录添加到包含链接表的查询中? - How do I add custom records into a query containing a linked table? 如何根据字段值执行查询更新或插入 - How can i Execute query as update or insert on the basis of field value 查找MySQL查询结果的字段的最大值 - finding the maximum value of a field of a MySQL query result 如果字段具有相同的值并添加新的计数器字段,如何实现将输出分组的SQL查询? - How can I implement an SQL query that group my output if the fields have the same values and add a new counter field? 如何基于两个字段的最大值作为第三个字段的子集的 select 记录? - How to select records based on max value of two fields as a subset of third field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM