SQL Group by max

时间:2014-08-28 02:34:36

标签: sql mariadb

我正在使用MariaDB 5.5并且在选择查询时遇到问题。我正在尝试在一列中选择两个日期时间之间的值,并按另一列中的最大日期时间进行分组。表结构类似于

|          Model_Init |          Model_Time | Location_ID | Var_ID | Value |
| ------------------- | ------------------- | ------------| ------ | ----- |
| 2014-08-22 06:00:00 | 2014-08-23 04:00:00 |      123456 |     23 |  76.2 |
| 2014-08-22 06:00:00 | 2014-08-23 05:00:00 |      123456 |     23 |  77.2 |
| 2014-08-22 06:00:00 | 2014-08-23 06:00:00 |      123456 |     23 |  78.2 |
| 2014-08-22 06:00:00 | 2014-08-23 07:00:00 |      123456 |     23 |  79.2 |
| 2014-08-22 06:00:00 | 2014-08-23 08:00:00 |      123456 |     23 |  80.2 |
| 2014-08-23 06:00:00 | 2014-08-23 06:00:00 |      123456 |     23 |  81.2 |
| 2014-08-23 06:00:00 | 2014-08-23 07:00:00 |      123456 |     23 |  82.2 |
| 2014-08-23 06:00:00 | 2014-08-23 08:00:00 |      123456 |     23 |  83.2 |
| 2014-08-23 06:00:00 | 2014-08-23 09:00:00 |      123456 |     23 |  84.2 |
| 2014-08-23 06:00:00 | 2014-08-23 10:00:00 |      123456 |     23 |  85.2 |
| 2014-08-23 06:00:00 | 2014-08-23 11:00:00 |      123456 |     23 |  86.2 |

我想在两个Model_Time值之间选择连续的一组时间,即:

SELECT * 
FROM WRF_data_norm 
WHERE Location_ID=123456 AND 
        Var_ID=23 AND 
        Model_Time>='2014-08-23 04:00:00' AND 
        Model_Time<='2014-08-23 10:00:00'
GROUP BY Model_Time;

哪个可能会返回结果:

|          Model_Init |          Model_Time | Location_ID | Var_ID | Value |
| ------------------- | ------------------- | ------------| ------ | ----- |
| 2014-08-22 06:00:00 | 2014-08-23 04:00:00 |      123456 |     23 |  76.2 |
| 2014-08-22 06:00:00 | 2014-08-23 05:00:00 |      123456 |     23 |  77.2 |
| 2014-08-22 06:00:00 | 2014-08-23 06:00:00 |      123456 |     23 |  78.2 |
| 2014-08-22 06:00:00 | 2014-08-23 07:00:00 |      123456 |     23 |  79.2 |
| 2014-08-22 06:00:00 | 2014-08-23 08:00:00 |      123456 |     23 |  80.2 |
| 2014-08-23 06:00:00 | 2014-08-23 09:00:00 |      123456 |     23 |  84.2 |
| 2014-08-23 06:00:00 | 2014-08-23 10:00:00 |      123456 |     23 |  85.2 |
| 2014-08-23 06:00:00 | 2014-08-23 11:00:00 |      123456 |     23 |  86.2 |

我希望Model_Time的不同值具有Model_Init的最大值,即:

|          Model_Init |          Model_Time | Location_ID | Var_ID | Value |
| ------------------- | ------------------- | ------------| ------ | ----- |
| 2014-08-22 06:00:00 | 2014-08-23 04:00:00 |      123456 |     23 |  76.2 |
| 2014-08-22 06:00:00 | 2014-08-23 05:00:00 |      123456 |     23 |  77.2 |
| 2014-08-23 06:00:00 | 2014-08-23 06:00:00 |      123456 |     23 |  81.2 |
| 2014-08-23 06:00:00 | 2014-08-23 07:00:00 |      123456 |     23 |  82.2 |
| 2014-08-23 06:00:00 | 2014-08-23 08:00:00 |      123456 |     23 |  83.2 |
| 2014-08-23 06:00:00 | 2014-08-23 09:00:00 |      123456 |     23 |  84.2 |
| 2014-08-23 06:00:00 | 2014-08-23 10:00:00 |      123456 |     23 |  85.2 |
| 2014-08-23 06:00:00 | 2014-08-23 11:00:00 |      123456 |     23 |  86.2 |

我不确定如何更改查询。

编辑:找到解决方案:

SELECT Max(Model_Init), Model_Time,Location_ID, Var_ID,Value FROM WRF_data_norm GROUP BY Model_Time, Location_ID, Var_ID, Value HAVING Location_ID=123456 AND Var_ID=23 AND Model_Time>='2014-09-23 04:00:00' AND Model_Time<='2014-09-23 11:00:00' ORDER BY Model_Time;

1 个答案:

答案 0 :(得分:0)

试试这个: -

SELECT   Model_Init, Model_Time, Location_ID, Var_ID, MAX(Value)
FROM WRF_data_norm 
WHERE Location_ID=123456 AND 
Var_ID=23 AND 
Model_Time>='2014-08-23 04:00:00' AND 
Model_Time<='2014-08-23 10:00:00'
GROUP BY Model_Init, Model_Time, Location_ID, Var_ID;

这可能对您有所帮助。