简体   繁体   English

MySql:查找最大SUM值?

[英]MySql : finding MAX of SUM values?

I would like to find out the highest quantity of the item of each category. 我想找出每个类别中项目的最高数量。

I have a table named "products" as shown below : 我有一个名为“产品”的表,如下所示:

+----------+-------+-----+
| Category | Item  | Qty |
+----------+-------+-----+
| A        | A100  |  15 |
| A        | A100  |   7 |
| A        | A101  |   3 |
| A        | A101  |   9 |
| B        | B100  |   6 |
| B        | B100  |   7 |
| B        | B101  |   8 |
| B        | B101  |  13 |
+----------+-------+-----+

After I execute the query 执行查询后

SELECT Category, Item, SUM(Qty) as Quantity FROM products GROUP BY Category,Item;

I got the results as below 我得到如下结果

+----------+-------+----------+
| Category | Item  | Quantity |
+----------+-------+----------+
| A        | A100  |       22 |
| A        | A101  |       12 |
| B        | B100  |       13 |
| B        | B101  |       21 |
+----------+-------+----------+

Now I want to find the highest quantity of the item in each category, which I want to have my output as follows : 现在,我想找到每个类别中该项目的最大数量,我希望其输出如下:

+----------+-------+----------+
| Category | Item  | Quantity |
+----------+-------+----------+
| A        | A100  |       22 |
| B        | B101  |       21 |
+----------+-------+----------+

how can I do that? 我怎样才能做到这一点? I have tried the query below but I only got 'OK' as the result without giving me any output table. 我已经尝试过下面的查询,但是结果是“ OK”,而没有给我任何输出表。

select Category, Item, max(Qty) as Quantity from products group by OD_type  having Quantity = (select sum(Qty) from products group by Item);

How can I get my desired output as shown in the table above? 如上表所示,如何获得所需的输出? I am sorry if this is not a good question or my question is not clear enough. 很抱歉,如果这不是一个好问题,或者我的问题不够清楚。 Any help will be appreciated, Thank you! 任何帮助将不胜感激,谢谢!

This would be easier to use a view or a common-table-expression (if mysql would support it). 使用viewcommon-table-expression (如果mysql支持的话)会更容易。

SELECT t.category, t.item, t.quantity
FROM (
    SELECT Category, Item, SUM(Qty) Quantity 
    FROM products 
    GROUP BY Category, Item
  ) t JOIN (
    SELECT category, max(quantity) Quantity
    FROM (
      SELECT Category, Item, SUM(Qty) Quantity 
      FROM products 
      GROUP BY Category,Item
    ) t
    GROUP BY category
  ) t2 ON t.Category = t2.Category and t.quantity = t2.quantity

The basic idea is to get the sum grouped by each category and item, then to get the max(sum) by each category, and then join back to the original sum query... 基本思想是获取按每个类别和项目分组的sum ,然后按每个类别获取max(sum) ,然后重新join原始的sum查询...

you need to use it as subquery , in case there are two items with same max value, you will get only one of them. 您需要将其用作子查询,以防有两个项目具有相同的最大值,您只会得到其中之一。

SELECT Category, Item, Max(Quantity) as MaxVal
FROM
(
SELECT Category, Item, SUM(Qty) as Quantity 
FROM products 
GROUP BY Category,Item
) T
GROUP BY Category

This looks ugly and I'm seeing if mysql has a better way... but I believe it works... 这看起来很难看,我正在查看mysql是否有更好的方法...但是我相信它可以工作...

SELECT A.category, A.Item, A.Quantity
FROM (SELECT Category, Item, SUM(Qty) as Quantity 
      FROM products 
      GROUP BY Category,Item) A
INNER JOIN (Select Max(B.quantity) Quantity, B.Category 
            FROM 
              (SELECT Category, SUM(Qty) as Quantity 
               FROM products 
               GROUP BY Category, Item) B
            GROUP BY Category) C
  ON A.Category=C.Category
 and A.Quantity = C.Quantity

I want to use a window set but it appears mysql doesn't support them. 我想使用一个窗口集,但是mysql不支持它们。 And since we have to aggregate the data first before we can get the max, the inner select is necessary.. and I wanted to use a CTE to improve readability, but MySQL doesn't support them either... 而且由于我们必须先汇总数据才能获得最大值,因此内部选择是必需的..我想使用CTE来提高可读性,但是MySQL也不支持它们。

I would probably create tables A, B as a View and call the view to make this easier to read long run. 我可能会创建表A,B作为视图,并调用该视图以使其更易于长期读取。

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

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