简体   繁体   English

SQL GROUP BY和MAX

[英]SQL GROUP BY AND MAX


I have the following table in my database: 我的数据库中有下表:

-------------------------------------------
| value  | category |         date        |
-------------------------------------------
| 12.2   | A        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------
| 13.3   | A        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------
| 11.0   | B        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------
| 11.2   | C        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------
| 19.2   | C        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------
| 10.4   | C        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------

And i'm trying to get the Maximum Value(12.2, 13.3,..) and the date correspondant to this value of each Category(A, B, C). 我正在尝试获取最大值(12.2,13.3,..)和与每个Category(A,B,C)的该值相对应的日期。

Like this for example: 例如:

-------------------------------------------
| value  | category |         date        |
-------------------------------------------
| 13.3   | A        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------
| 11.0   | B        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------
| 19.2   | C        | yyyy-MM-dd HH:mm:ss | 
-------------------------------------------

I have followed this link SQL: How to select a max value for each group per day? 我遵循此链接SQL:如何每天为每个组选择最大值?

So I made this query: 所以我做了这个查询:

SELECT MAX(value), category, date FROM myTable GROUP BY category;

But for some reason i don't know why it doesn't gave me what i'm looking for, it gave's me a wrong value of the DATE (DATE does not correspondant to the Max(value))!!! 但是由于某种原因,我不知道为什么它没有给我我想要的东西,它给了我错误的DATE值(DATE不对应于Max(value))!

You can do this using a sub qyery. 您可以使用sub qyery进行此操作。

SELECT MAX(t.value), 
    t.category,
    (SELECT m.date FROM myTable as m 
     WHERE m.value = MAX(t.value) AND m.category = t.category) as Category
FROM myTable as t
GROUP BY category;

But for some reason i don't know why it doesn't gave me what i'm looking for, it gave's me a wrong value 但是由于某种原因,我不知道为什么它没有给我我想要的东西,它给了我一个错误的值

The reason it didn't give you the correct values in this case: 在这种情况下,它没有为您提供正确值的原因:

SELECT MAX(value), category, date FROM myTable GROUP BY category;

is that, when you include a columns in the select clause in mysql that are not included in the group by nor an aggregate function like date , mysql selects an arbitrary values for it, so that it gives you the wrong values not the values that corresponds to the max values. 就是说,当您在mysql的select子句中包含不包含在group by或类似date的聚合函数时,mysql会为其选择一个任意值,这样它会为您提供错误的值,而不是对应的值达到最大值。


And i'm trying to get the Maximum Value(12.2, 13.3,..) and the date correspondant to this value of each Category(A, B, C). 我正在尝试获取最大值(12.2,13.3,..)和每个Category(A,B,C)的该值相对应的日期

To get the date s that are correspondant to the MAX(value) try this instead: 要获取与MAX(value)相对应的date ,请尝试以下方法:

SELECT t1.*
FROM myTable AS t1
INNER JOIN
(
  SELECT MAX(value) AS MaxV, category
  FROM myTable AS t1
  GROUP BY category
) AS t2 ON  t2.MaxV     = t1.value
        AND t1.Category = t2.category;

The join to the subquery will ensure that the returned date value correspondant to the Max(value) and will eliminate the other values. 子查询的联接将确保返回的日期值与Max(value)相对应,并将消除其他值。

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

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