简体   繁体   English

从MySQL中选择行并使用MAX和MIN进行分组

[英]Select rows from MySQL and grouping use MAX and MIN

I have the following table called 'ArchiveTable': 我有一个名为“ ArchiveTable”的下表:

+---------+-----------------+---------+-----------------+--------------+------------------+
| maxtemp | maxtemptime     | mintemp | mintemptime     | minwindchill | minwindchilltime |
+---------+-----------------+---------+-----------------+--------------+------------------+
| 27.9    | 3/17/2015 16:55 | 25.8    | 3/17/2015 19:00 | 25.8         | 3/17/2015 19:00  |
+---------+-----------------+---------+-----------------+--------------+------------------+
| 25.7    | 3/17/2015 19:05 | 19.3    | 3/18/2015 9:05  | 19.3         | 3/18/2015 9:05   |
+---------+-----------------+---------+-----------------+--------------+------------------+
| 23.1    | 3/18/2015 19:05 | 18.7    | 3/19/2015 6:30  | 18.7         | 3/19/2015 6:30   |
+---------+-----------------+---------+-----------------+--------------+------------------+

I have to select the maximum value of 'maxtemp' and its corresponding 'maxtemptime' date, minimum value of 'mintemp' and its corresponding date, and minimum value of 'minwindchill' and its corresponding date. 我必须选择“ maxtemp”的最大值及其对应的“ maxtemptime”日期,“ mintemp”的最小值及其对应的日期以及“ minwindchill”的最小值及其对应的日期。

I know how to obtain the max and min values with the MAX() and MIN() functions, but I cannot associate these values to the corresponding date. 我知道如何使用MAX()MIN()函数获取最大值和MIN() ,但是我无法将这些值关联到相应的日期。

See this MySQL Handling of GROUP BY 请参阅此MySQL处理GROUP BY

If I understood you correctly you should do something like this 如果我对您的理解正确,则应执行以下操作

SELECT field1, field2, fieldN, COUNT(field1) AS alias FROM table
  GROUP BY field1
 HAVING maxtemp  = MAX(maxtemp); -- I think this is not correct

Although I'm not 100% sure about that solution you could try this as well 尽管我不确定该解决方案的100%,您也可以尝试

SELECT field1, field2, fieldN, COUNT(field1) AS alias FROM table
  GROUP BY field1
 HAVING maxtemp  = (SELECT MAX(maxtemp) FROM table);

If you could take the values on separate rows, then you could do something like this: 如果可以将值分别放在不同的行上,则可以执行以下操作:

(select a.* from archivetable order by maxtemp limit 1) union
(select a.* from archivetable order by maxtemp desc limit 1) union
. . .

Otherwise, if you can do something like this: 否则,如果您可以执行以下操作:

select atmint.mintemp, atmint.mintempdate,
       atmaxt.maxtemp, atmaxt.maxtempdate,
       atminwc.minwindchill, atminwc.minwindchilldate
from (select min(mintemp) as mintemp, max(maxtemp) as maxtemp, min(minwindchill) as minwindchill
      from archivetable
     ) a join
     archivetable atmint
     on atmint.mintemp = a.mintemp join
     archivetable atmaxt
     on atmaxt.maxtemp = a.maxtemp join
     archivetable atminwc
     on atminwc.minwindchill = a.minwindchill
limit 1;

The limit 1 is because multiple rows might have the same values. limit 1是因为多个行可能具有相同的值。 If so, you can arbitrarily choose one of them, based on how your question is phrased. 如果是这样,您可以根据问题的表达方式任意选择其中之一。

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

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