简体   繁体   English

MySql:查找按列分组的SUM的最大值

[英]MySql: Finding the MAX value of the SUM grouped by column

I have a table that includes names, items, types, and a count per item. 我有一个表,其中包含名称,项目,类型以及每个项目的计数。 I need to find a way to determine the name that has the MAX count in each type. 我需要找到一种方法来确定每种类型中具有最大数量的名称。

Example table: 表格示例:

---------------------------------------
| name | item     | type      | count |
| ------------------------------------|
| Dave | carrot   | vegetable | 2     |
| Dave | broccoli | vegetable | 3     |
| Tom  | spinach  | vegetable | 2     |
| Jon  | swiss    | cheese    | 3     |
| Mark | cheddar  | cheese    | 5     |
| Jon  | cheddar  | cheese    | 6     |
| Tony | onion    | vegetable | 3     |

I want to find the names of each person with the highest SUM(count) in each type. 我想找到每种类型中SUM(count)最高的每个人的名字。 This is my expected result: 这是我的预期结果:

----------------------------
| name | type      | count |
| -------------------------|
| Dave | vegetable | 5     |
| Jon  | cheese    | 9     |

I'm trying to see if there is an elegant way of doing it other than queries for every name and then manually calculating the MAX. 我正在尝试查看是否有一种优雅的方法,除了查询每个名称,然后手动计算MAX。

This may not be optimal, but it's pretty straightforward: 这可能不是最佳选择,但很简单:

SELECT name, type, MAX(count) as count FROM (
   SELECT name, type, SUM(count) as count
    FROM mytable
    GROUP BY type, name
) as rank
GROUP BY type;

The inner query returns each name and the sum of the type for that name, like: 内部查询返回每个名称和该名称的类型总和,例如:

NAME    TYPE    COUNT
Jon     cheese      9
Dave    vegetable   5
Mark    cheese      5
Tony    vegetable   3
Tom     vegetable   2

Then the outer query groups by type, thus only returning one name per type, and the having max() returns the top name for the type. 然后外部查询按类型分组,因此每种类型仅返回一个名称,而具有max()的则返回该类型的顶级名称。

This will not return tied names, but will return the same name for different types, so if suddenly gave Dave 10 in swiss, he'd show up as the top for vegetable and cheese. 这不会返回并列名称,但是会为不同类型返回相同名称,因此,如果突然给了瑞士的戴夫10名,他将成为蔬菜和奶酪行业的佼佼者。

First calculate the totals for each name/category combination. 首先计算每个名称/类别组合的总数。 Do this either in a subquery or a view. 在子查询或视图中执行此操作。 I'm going to choose a view to reduce code bloat, since it will be needed more than once. 我将选择一种视图来减少代码膨胀,因为将需要多次。

CREATE VIEW totals AS
SELECT name, type, SUM(count) AS count
FROM yourtable
GROUP BY name, type
----------------------------
| name | type      | count |
| -------------------------|
| Dave | vegetable | 5     |
| Tony | vegetable | 3     |
| Tom  | vegetable | 2     |
| Jon  | cheese    | 9     |
| Mark | cheese    | 5     |
----------------------------

Now you need to find the maximum count for each type. 现在,您需要找到每种类型的最大数量。

SELECT type, MAX(count) AS max_count
FROM totals
GROUP BY type
-------------------------
| type      | max_count |
| -----------------------
| vegetable | 5         |
| cheese    | 9         |
-------------------------

Now you just need to query the view totals for all rows where (type, count) is equal to any of the pairs in the second result set. 现在,您只需要查询(类型,计数)等于第二个结果集中的任何对的所有行的视图totals I've already done most of the work for you. 我已经为您完成了大部分工作。 I'll leave you to finish it off. 我让你结束。

Without having the database in front of me, give this a shot: 在没有数据库的情况下,请尝试一下:

SELECT * FROM Table T 
  LEFT JOIN (SELECT type, MAX(count) AS max FROM Table GROUP BY type) AS maxType 
  ON T.type = maxType.type 
    AND t.count = max.count

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

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