简体   繁体   English

使用 subQuery SQL 选择最大值

[英]Select the max Value with subQuery SQL

Using the below query, I am selecting the ItemName, Description from Items Table and QuantityInStock, CurrentPrice from ItemsStock Table.使用以下查询,我从Items表中选择 ItemName、Description 和 QuantityInStock、从ItemsStock表中选择 CurrentPrice。 And also trying to fetch the VendorName for each Item from Vendors Table using PurchaseInvoices and ItemsReceived Table Relation.而且还试图获取VendorName从每个项目Vendors使用表PurchaseInvoicesItemsReceived表关系。 Now the problem is I want to fetch the VENDOR who provided the MOST QUANTITY for a particular item.现在的问题是我想获取为特定项目提供最多数量的供应商

I mean let's say an ITEM ABC was provided by 3 VENDORS V1, V2, and V3.我的意思是让我们说一个ITEM ABC是由 3 个 V1、V2 和 V3 供应商提供的。 V3 provided 90 times (He provided most quantity). V3提供了90次(他提供的数量最多)。 Now I want to show him in VendorName column.现在我想在VendorName列中显示他。 Likewise for each item show the vendor who provided that item most times.同样,对于每个项目,显示提供该项目次数最多的供应商。

Query I am using (I think this subquery needs to be changed)我正在使用的查询(我认为这个子查询需要更改)

SELECT Items.Name
    ,Max(Items.Description) AS Description
    ,Max(ItemsStock.Quantity) AS QuantityInStock
    ,Max(ItemsStock.CurrentPrice) AS CurrentPrice
    ,Max(Vendors.VendorName)
FROM ItemsSold
INNER JOIN Items ON ItemsSold.ItemSoldID = Items.ItemID
INNER JOIN ItemsStock ON ItemsSold.ItemSoldID = ItemsStock.ItemID
INNER JOIN Vendors ON Vendors.VendorID = (
        SELECT max(PI.VendorID)
        FROM ItemReceived IR
        JOIN PurchaseInvoices PI ON IR.PurchaseInvoiceID = PI.PurchaseInvoiceID
        WHERE IR.ItemID = Items.ItemID
        GROUP BY IR.ItemID
        )
GROUP BY Items.Name

Something like this:像这样的东西:

Select 
  Items.Name,
  Items.Description,
  ItemsStock.Quantity,
  ItemsStock.CurrentPrice,
  Vendors.VendorName
From 
ItemsSold
INNER Join Items On ItemsSold.ItemSoldID = Items.ItemID
INNER JOIN ItemsStock On ItemsSold.ItemSoldID = ItemsStock.ItemID
INNER JOIN 
(
  SELECT *, ROW_NUMBER() OVER(PARTITION BY a.ItemID ORDER BY sumProvided DESC) rown FROM
  (
    SELECT PI.VendorID, IR.ItemID, SUM(IR.Quantity) as sumProvided
    FROM ItemReceived IR
    INNER JOIN PurchaseInvoices PI on IR.PurchaseInvoiceID = PI.PurchaseInvoiceID
    GROUP BY PI.VendorID, IR.ItemID
  ) a
) v
ON v.ItemID = Items.ItemID AND v.rown = 1
INNER JOIN Vendors ON Vendors.VendorID = v.VendorID

Here there's an inner query that sums up the supplies of a particular item by each vendor, then an outer query that numbers the rows with 1 = the highest supplier这里有一个内部查询,它总结了每个供应商对特定项目的供应,然后是一个外部查询,用 1 = 最高供应商对行进行编号

In the outermost query we cut it to just the rows that were rown=1 (the most vending vendor在最外层的查询中,我们将其剪切为 rown=1(自动售货最多的供应商)的行

I removed the outer group by because I can't see how it makes sense to aggregate these things.我删除了外部组,因为我看不出将这些东西聚合起来有什么意义。 You should note that your question is hard to write a test for, because you haven't included sample data and expected output.. but this is a general tactic for "find the highest X and return other column Y from the same row"您应该注意,您的问题很难编写测试,因为您没有包含示例数据和预期输出..但这是“找到最高的 X 并从同一行返回其他列 Y”的一般策略

You will have to replace SUM(PI.PURCHASE_QUANTITY) with something relevant yourself - you didn't give any clues as to what the structure of the PI table was您将不得不用自己相关的东西替换SUM(PI.PURCHASE_QUANTITY) - 您没有提供任何关于 PI 表结构的线索

I presume that going from v.VendorID to Vender.VendorName will be easy enough for you to do我认为从 v.VendorID 到 Vender.VendorName 对你来说很容易做到


OK, so - debugging SQLs 101好的,那么 - 调试 SQL 101

I assert that this should work:我断言这应该有效:

Select 
  *
From 
--ItemsSold
--INNER Join Items On ItemsSold.ItemSoldID = Items.ItemID
--INNER JOIN ItemsStock On ItemsSold.ItemSoldID = ItemsStock.ItemID
--INNER JOIN
(
  SELECT *, ROW_NUMBER() OVER(PARTITION BY a.ItemID ORDER BY sumProvided DESC) rown FROM
  (
    SELECT PI.VendorID, IR.ItemID, SUM(IR.Quantity) as sumProvided
    FROM ItemReceived IR
    INNER JOIN PurchaseInvoices PI on IR.PurchaseInvoiceID = PI.PurchaseInvoiceID
    GROUP BY PI.VendorID, IR.ItemID
  ) a
) v
--ON v.ItemID = Items.ItemID AND v.rown = 1

I just put a SELECT *, then stripped out all the tables apart from one - the one that is doing th most work.我只是放了一个 SELECT *,然后把所有的表都去掉了,除了一个表——那个做最多工作的表。 So.. Does it return any records?那么..它是否返回任何记录?

If not, then the join is faulty (there are no records in PI that relate to IR), or there really are no records in PI or IR如果不是,则连接有问题(PI 中没有与 IR 相关的记录),或者 PI 或 IR 中确实没有记录

Fix it so this returns rows修复它以便返回行

Next question Does the query return a 1 in rown column, for the suppliers who shipped you the most of a particular item?下一个问题对于向您运送特定物品最多的供应商,该查询是否在 rown 列中返回 1?

If not, something is wonky with the records - take a look.如果没有,则记录有问题 - 看一看。 If so, we're all good如果是这样,我们都很好

Join the other tables back in one at a time:一次将其他表合并为一个:

Select 
  *
From 
--ItemsSold
/*INNER Join*/ Items /*On ItemsSold.ItemSoldID = Items.ItemID*/
--INNER JOIN ItemsStock On ItemsSold.ItemSoldID = ItemsStock.ItemID
INNER JOIN
(
  SELECT *, ROW_NUMBER() OVER(PARTITION BY a.ItemID ORDER BY sumProvided DESC) rown FROM
  (
    SELECT PI.VendorID, IR.ItemID, SUM(IR.Quantity) as sumProvided
    FROM ItemReceived IR
    INNER JOIN PurchaseInvoices PI on IR.PurchaseInvoiceID = PI.PurchaseInvoiceID
    GROUP BY PI.VendorID, IR.ItemID
  ) a
) v
ON v.ItemID = Items.ItemID AND v.rown = 1

Keep going until you suddenly lose records that you weren't expecting would disappear...继续前进,直到您突然丢失了您没想到会消失的记录...

:

Select 
  *
From 
ItemsSold
INNER Join Items On ItemsSold.ItemSoldID = Items.ItemID
--INNER JOIN ItemsStock On ItemsSold.ItemSoldID = ItemsStock.ItemID
INNER JOIN
(
  SELECT *, ROW_NUMBER() OVER(PARTITION BY a.ItemID ORDER BY sumProvided DESC) rown FROM
  (
    SELECT PI.VendorID, IR.ItemID, SUM(IR.Quantity) as sumProvided
    FROM ItemReceived IR
    INNER JOIN PurchaseInvoices PI on IR.PurchaseInvoiceID = PI.PurchaseInvoiceID
    GROUP BY PI.VendorID, IR.ItemID
  ) a
) v
ON v.ItemID = Items.ItemID AND v.rown = 1

Maybe all the items in "items sold" and all the items in "items received" don't match up!可能“已售商品”中的所有商品和“已收到商品”中的所有商品都不匹配! (Maybe you only ever sold stuff you haven't replaced, or sold things you don't have any purchase records for/existing inventory that is in Items that is received but never sold, or sold but never received) (也许你只卖过你没有更换过的东西,或者卖过你没有任何购买记录的东西/现有库存中的物品,这些物品在收到但从未售出,或已售出但从未收到过)

you can simply join with subquery你可以简单地加入子查询

 Select Items.Name,Max(Items.Description) as Description ,
Max(ItemsStock.Quantity) as QuantityInStock,Max(ItemsStock.CurrentPrice)as CurrentPrice,V.VendorName
From ItemsSold
INNER Join Items On ItemsSold.ItemSoldID = Items.ItemID
INNER JOIN ItemsStock On ItemsSold.ItemSoldID = ItemsStock.ItemID
INNER JOIN  (
select max(PI.VendorID) VendorID,IR.ItemID
from ItemReceived IR
JOIN PurchaseInvoices PI on IR.PurchaseInvoiceID = PI.PurchaseInvoiceID
group by IR.ItemID
) Vendors ON Vendors.ItemID=Items.ItemID 
join Vendors v on v.VendorID=Vendors.VendorID
Group By Items.Name,V.VendorName

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

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