简体   繁体   English

如何使用MAX()返回具有最大值的行?

[英]How do I use MAX() to return the row that has the max value?

I have table orders with fields id , customer_id and amt : 我有包含字段idcustomer_idamtorders

SQL Fiddle SQL小提琴

And I want get customer_id with the largest amt and value of this amt . 我希望得到customer_id这个最大AMT和价值amt

I made the query: 我提出了查询:

SELECT customer_id, MAX(amt) FROM orders;

But the result of this query contained an incorrect value of customer_id . 但是此查询的结果包含不正确的customer_id值。

Then I built such the query: 然后我构建了这样的查询:

SELECT customer_id, MAX(amt) AS maximum FROM orders GROUP BY customer_id ORDER BY maximum DESC LIMIT 1;

and got the correct result. 并得到了正确的结果。

But I do not understand why my first query not worked properly . 但我不明白为什么我的第一个查询无法正常工作 What am I doing wrong? 我究竟做错了什么?

And is it possible to change my second query to obtain the necessary information to me in a simpler and competent way? 是否有可能更改我的第二个查询以更简单和更有能力的方式获取必要的信息?

MySQL will allow you to leave GROUP BY off of a query, thus returning the MAX(amt) in the entire table with an arbitrary customer_id . MySQL将允许您将GROUP BY从查询中MAX(amt) ,从而使用任意customer_id返回整个表中的MAX(amt) Most other RDBMS require the GROUP BY clause when using an aggregate. 大多数其他RDBMS在使用聚合时需要GROUP BY子句。

I don't see anything wrong with your 2nd query -- there are other ways to do it, but yours will work fine. 我没有看到你的第二个查询有什么问题 - 还有其他方法可以做到,但你的工作正常。

Some versions of SQL give you a warning or error when you select a field, have an aggregate operator like MAX or SUM , and the field you are selecting does not appear in GROUP BY . 某些版本的SQL在您选择字段时会发出警告或错误,具有MAXSUM等聚合运算符,并且您选择的字段不会出现在GROUP BY

You need a more complicated query to fetch the customer_id corresponding to the max amt. 您需要一个更复杂的查询来获取与max amt相对应的customer_id。 Unfortunately SQL is not as naive as you think. 不幸的是,SQL并不像你想象的那么幼稚。 Once such way to do this is: 一旦这样做的方式是:

select customer_id from orders where amt = ( select max(amt) from orders);

Although a solution using joins is likely more performant. 虽然使用连接的解决方案可能更具性能。

To understand why what you were trying to do doesn't make sense, replace MAX with SUM . 要理解为什么你要做的事情没有意义,用SUM替换MAX From the stance of how aggregate operators are interpreted, it's a mere coincidence that MAX returns something that corresponds to an actual row. 从如何解释聚合运算符的立场来看, MAX返回与实际行相对应的东西仅仅是巧合。 SUM does not have this property, for instance. SUM没有此属性。

Practically your first query can be seen as if it were GROUP BY-ed into a big single group. 实际上,您的第一个查询可以看作是GROUP BY-ed成为一个大的单个组。 Also, MySQL is free to choose each output value from different source rows from the same group. 此外,MySQL可以自由选择来自同一组的不同源行的每个输出值。

http://dev.mysql.com/doc/refman/5.7/en/group-by-extensions.html http://dev.mysql.com/doc/refman/5.7/en/group-by-extensions.html

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. MySQL扩展了GROUP BY的使用,因此选择列表可以引用GROUP BY子句中未命名的非聚合列。 The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. 服务器可以自由选择每个组中的任何值,因此除非它们相同,否则所选的值是不确定的。 Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. 此外,添加ORDER BY子句不会影响每个组中值的选择。 Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values within each group the server chooses. 选择值后会对结果集进行排序,而ORDER BY不会影响服务器选择的每个组中的值。

The problem with MAX() is that it will select the highest value of that specified field, considering the specified field alone. MAX()的问题在于它将选择该指定字段的最高值,仅考虑指定的字段。 The other values in the same row are not considered or given preference for the result at any degree. 在任何程度上,不考虑或优先考虑同一行中的其他值。 MySQL will usually return whatever value is the first row of the GROUP (in this case the GROUP is composed by the entire table sinse no group was specified), dropping the information of the other rows during the agregation. MySQL通常会返回GROUP的第一行的任何值(在这种情况下, GROUP由整个表组成,没有指定组),在聚合期间丢弃其他行的信息。

To solve this, you could do that: 要解决这个问题,你可以这样做:

SELECT customer_id, amt FROM orders ORDER BY amt DESC LIMIT 1

It should return you the customer_id and the highest amt while preserving the relation between both, because no agregation was made. 它应该返回customer_id和最高amt同时保留两者之间的关系,因为没有进行聚合。

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

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