简体   繁体   English

在GROUP BY分组中,根据另一列的最大值选择值

[英]Within GROUP BY grouping, select value based on highest value of another column

I am attempting to build a query that reduces a GROUP BY group to a single row, including a value for a column based on the max value of another column. 我正在尝试建立一个查询,以将GROUP BY组减少为一行,包括基于另一列的最大值的一列值。 In this case, I want an item id, total qty ordered and most-used supplier. 在这种情况下,我需要一个商品ID,订购的总数量和最常用的供应商。

I've successfully built a query that sums the qty ordered and groups by item and supplier, yielding: 我已经成功构建了一个查询,该查询汇总了按物料和供应商订购的数量和组,得出:

| id | qty | supplier       |
|  1 | 20  | S&S Activewear |
|  1 | 10  | J&J Textiles   |
|  2 | 5   | AB Footwear    |
|  2 | 10  | CD Shoes       |

and the intended result would be total qty ordered (for all suppliers) and most used supplier, so: 并且预期结果将是订购的总数量(对于所有供应商)和最常用的供应商,因此:

| id | total_qty | most_used_supplier |
|  1 | 30        | S&S Activewear     |
|  2 | 15        | CD Shoes           |

Conceptually, I imagine doing a subquery, grouping the above results by id alone, then sum(qty) and somehow choose the supplier value by ranking the GROUP BY by qty. 从概念上讲,我想做一个子查询,将上述结果单独按id分组,然后按sum(qty)分组,然后通过按数量对GROUP BY排序,从而以某种方式选择供应商价值。

I have read many related posts but I am failing to apply any of those methods successfully to this end, including use of ROW_NUMBER and PARTITION_BY. 我已经阅读了许多相关的文章,但是未能成功地将任何这些方法应用到此目的,包括使用ROW_NUMBER和PARTITION_BY。

I am doing this in Elixir with Ecto on a Postgres DB, but to keep it generalized so anyone can respond, I am just looking to understand how this would be done in SQL. 我正在Elixir上与Postgres DB上的Ecto一起执行此操作,但是为了使它通用化以便任何人都可以响应,我只是想了解如何在SQL中完成此操作。 Please let me know if I can provide more detail, thank you. 如果可以提供更多详细信息,请告诉我,谢谢。

I'm going to suggest multiple subqueries: 我将建议多个子查询:

select id, sum(qty),
       (select t2.supplier
        from t t2
        where t2.id = t.id
        order by t2.qty desc
        fetch first 1 row only
       ) as supplier
from t
group by id;

This uses standard syntax for returning one row. 这使用标准语法返回一行。 Your database may have another syntax for the equivalent of fetch first 1 row only . 您的数据库可能具有另一种语法, fetch first 1 row only相当于fetch first 1 row only

First find biggest quantities for each id. 首先为每个编号找到最大数量。 Then find appropriate suppliers which provide those biggest quantities. 然后找到提供最大数量的合适供应商。 Here issue may appear if there are more then one "biggest", and you have to see how to deal with it. 如果存在多个“最大”问题,则可能会出现此问题,您必须查看如何处理它。 Finally, just join it once more to same table, adding appropriate quantity sums. 最后,再次将其加入同一张表,添加适当的数量总和。

SELECT item.id, sum(item.qty) total_qty, biggestSupplier.supplier most_used_supplier
from item join
(
    SELECT item.id, supplier       
    from item
    JOIN 
    (
        SELECT id, max(qty) maxqty
        FROM item
        GROUP BY id
    ) maxQtyForId ON item.id = maxQtyForId.id AND item.qty = maxQtyForId.maxqty
) biggestSupplier ON item.id = biggestSupplier.id
group by item.id, biggestSupplier.supplier       

There are several approaches and it sounds like you've played with this one a bit even: 有几种方法,听起来您甚至都玩过这种方法:

with data as (
    select *,
        row_number() over (partition by id order by qty desc) as rn
    from T
)
select id, sum(qty) as total_qty,
    (select d2.supplier from data d2
        where s2.id = d.id and rn = 1) as most_used_supplier
from data d
group by id;

I Divide the problem in 2. First, finding the max qty and then adding up the qty. 我将问题划分为2。首先,找到最大数量,然后将数量相加。 Finally, Join the table to get the answers. 最后,加入表格以获取答案。

SELECT T4.ID, T5.sumQty AS total_qty,T4.supplier AS most_used_supplier
FROM [Test].[dbo].[Test] AS T4 LEFT JOIN
(
    SELECT ID,SUM(QTY) as sumQty
    FROM [Test].[dbo].[Test]
    GROUP BY ID
)AS T5
ON T4.ID = T5.ID
WHERE supplier IN
(
    SELECT supplier 
    FROM [Test].[dbo].[Test] AS T1 LEFT JOIN
        (
          SELECT MAX(qty) AS maxQty, ID
          FROM [Test].[dbo].[Test] AS T
          GROUP BY id
        ) AS T2
    ON T1.ID = T2.ID
    AND T1.qty = T2.maxQty
    WHERE T2.ID IS NOT NULL
)

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

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