如何按货运方式获取供应商并显示其订单数量最多的方式?

huangapple go评论59阅读模式
英文:

How to get Shipment method wise suppliers with most order quantity?

问题

以下是您提供的内容的中文翻译:

我在理解何时使用子查询以及如何以合理的方式实现它们方面遇到了困难。

我有以下表格:

supplier

供应商ID 名称
1 供应商#0000001
2 供应商#0000002
4 供应商#0000003
3 供应商#0000004
... ...

orderitem

订单ID 运输方式 供应商
1 TRUCK 134
2 MAIL 345
3 REG AIR 223
4 REG AIR 11345
5 RAIL 344
... ... 535

我想要找到每种运输方式下运送的“订单项”数量最多的供应商。所以我的最终结果应该是这样的:

运输方式 供应商名称 数量
TRUCK 供应商#0000002 120
MAIL 供应商#0003453 590
REG AIR 供应商#0003456 433
AIR 供应商#0000632 244
SHIP 供应商#0000654 566

我想出了这个查询:

SELECT 
    SHIPPINGMETHOD, SUPPLIERNAME, COUNT(ORDER_ID) AS AMOUNT
FROM 
    ORDERITEM
JOIN 
    SUPPLIER L ON L.SUPPLIER_ID = ORDERITEM.SUPPLIER
GROUP BY 
    SHIPPINGMETHOD, SUPPLIERNAME;

但是这只返回了每个运输方式下每个供应商的运送物品数量。

然后我尝试了这个查询:

SELECT 
    SHIPPINGMETHOD, L.SUPPLIERNAME, COUNT(OI.ORDER_ID) AS AMOUNT
FROM 
    ORDERITEM AS OI
JOIN 
    SUPPLIER L ON L.SUPPLIER_ID = OI.SUPPLIER
GROUP BY 
    OI.SHIPPINGMETHOD, L.SUPPLIERNAME
HAVING 
    COUNT(OI.ORDER_ID) = (SELECT MAX(AMOUNT)
                          FROM 
                              (SELECT 
                                   OI2.SHIPPINGMETHOD, L2.SUPPLIERNAME, 
                                   COUNT(OI2.ORDER_ID) AS AMOUNT
                               FROM 
                                   ORDERITEM AS OI2
                               JOIN 
                                   SUPPLIER L2 ON L2.SUPPLIER_ID = OI2.SUPPLIER
                               GROUP BY 
                                   OI.SHIPPINGMETHOD, L.SUPPLIERNAME) AS SUB
);

但我只得到了一行结果。这一行显示了一个方法的正确供应商和正确的运送物品数量,但我期望得到5行。我感觉我离答案很近,但实际上完全不清楚我的错误是什么。

英文:

I am having difficulties to understand when to use subqueries and how to implement them in a way that makes sense.

I have the following tables:

Table supplier:

supplier_id name
1 Supplier#0000001
2 Supplier#0000002
4 Supplier#0000003
3 Supplier#0000004
... ...

Table orderitem:

order_id shippingmethod supplier
1 TRUCK 134
2 MAIL 345
3 REG AIR 223
4 REG AIR 11345
5 RAIL 344
... ... 535

I want to find the supplier with the highest amount of shipped order_items for each shipping method. So my end result should look this:

shippingmethod suppliername amount
TRUCK Supplier#0000002 120
MAIL Supplier#0003453 590
REG AIR Supplier#0003456 433
AIR Supplier#0000632 244
SHIP Supplier#0000654 566

I came up with this:

SELECT 
    SHIPPINGMETHOD, SUPPLIERNAME, COUNT(ORDER_ID) AS AMOUNT
FROM 
    ORDERITEM
JOIN 
    SUPPLIER L ON L.SUPPLIER_ID = ORDERITEM.SUPPLIER
GROUP BY 
    SHIPPINGMETHOD, SUPPLIERNAME;

But this only returns the amount of shipped items of each supplier of each shipping method.

So then I tried this:

SELECT 
    SHIPPINGMETHOD, L.SUPPLIERNAME, COUNT(OI.ORDER_ID) AS AMOUNT
FROM 
    ORDERITEM AS OI
JOIN 
    SUPPLIER L ON L.SUPPLIER_ID = OI.SUPPLIER
GROUP BY 
    OI.SHIPPINGMETHOD, L.SUPPLIERNAME
HAVING 
    COUNT(OI.ORDER_ID) = (SELECT MAX(AMOUNT)
                          FROM 
                              (SELECT 
                                   OI2.SHIPPINGMETHOD, L2.SUPPLIERNAME, 
                                   COUNT(OI2.ORDER_ID) AS AMOUNT
                               FROM 
                                   ORDERITEM AS OI2
                               JOIN 
                                   SUPPLIER L2 ON L2.SUPPLIER_ID = OI2.SUPPLIER
                               GROUP BY 
                                   OI.SHIPPINGMETHOD, L.SUPPLIERNAME) AS SUB
);

I only get one row back. The row shows the correct supplier and the correct amount of shipped items for one of the methods, but I expected 5 rows. I feel like I am very close, but honestly have no clue at all what my mistake actually is.

答案1

得分: 1

以下是您要翻译的内容:

如果您想按运输方式找到订单数量最多的供应商,可以尝试以下方法:

** 在这里,使用了row_number()over()窗口函数,以订单数量降序排列供应商。

查询:

with cte as 
(
SELECT shippingmethod, L.name suppliername , COUNT(order_id) AS AMOUNT,
row_number()over(partition by shippingmethod, L.name order by COUNT(order_id) desc) as rn 
FROM orderitem
JOIN supplier L on L.supplier_id = orderitem.SUPPLIER
GROUP BY shippingmethod, L.name
)
select shippingmethod, suppliername , AMOUNT from cte
where rn=1

输出:

shippingmethod suppliername AMOUNT
MAIL Supplier#0000002 1
RAIL Supplier#0000004 1
REG AIR Supplier#0000004 1
TRUCK Supplier#0000001 1

fiddle

*** 如果有多个订单数量最多的客户,所有这些客户都将出现在列表中。

英文:

If you are trying to find shipping method wise suppliers with highest order quantity then you can try this:

** here row_number()over() window function is used to rank the supplier in descending order of order quantity.

Query:

with cte as 
(
SELECT shippingmethod, L.name suppliername , COUNT(order_id) AS AMOUNT,
row_number()over(partition by shippingmethod, L.name order by COUNT(order_id) desc) as rn 
FROM orderitem
JOIN supplier L on L.supplier_id = orderitem.SUPPLIER
GROUP BY shippingmethod, L.name
)
select shippingmethod, suppliername , AMOUNT from cte
where rn=1

Output:

shippingmethod suppliername AMOUNT
MAIL Supplier#0000002 1
RAIL Supplier#0000004 1
REG AIR Supplier#0000004 1
TRUCK Supplier#0000001 1

fiddle

*** If there are more than one customer with highest order quantity all of those would be in the list.

huangapple
  • 本文由 发表于 2023年5月18日 01:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定