使用subQuery SQL

时间:2019-04-23 09:47:23

标签: sql sql-server

使用以下查询,我从Items表中选择ItemName,Description,从ItemsStock表中选择QuantityInStock,CurrentPrice。并且尝试使用VendorNameVendors表关系从PurchaseInvoices表中获取每个项目的ItemsReceived。现在的问题是,我想获取为特定商品提供 MOST QUANTITY 的供应商。

我的意思是说,ITEM ABC由3个供应商V1,V2和V3提供,而V3提供了90次(他提供了最多的数量)。现在,我想在VendorName栏中显示他。就像明智的一样,为每个项目显示经常提供该项目的供应商。

我正在使用的查询(我认为此子查询需要更改)

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

2 个答案:

答案 0 :(得分:2)

类似这样的东西:

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

这里有一个内部查询,用于汇总每个供应商对特定物料的供应,然后是一个外部查询,该行对行编号为1 =最高供应商

在最外层的查询中,我们将其仅剪切为rown = 1(最畅销的供应商

我删除了外部组,因为我看不到聚合这些东西的意义。您应该注意,您的问题很难为其编写测试,因为您没有包括示例数据和预期的输出..但这是“查找最高的X并从同一行返回其他列Y”的一般策略。 / p>

您将不得不用自己感兴趣的东西代替SUM(PI.PURCHASE_QUANTITY)-您没有任何关于PI表结构的线索

我认为从v.VendorID到Vender.VendorName的转换很容易


好的,所以-调试SQL 101

我断言这应该可行:

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

我只是放一个SELECT *,然后将所有表从一个表中剥离出来-一个正在做最多的工作。所以.. 它返回任何记录吗?

如果不是,则表明连接有问题(PI中没有与IR相关的记录),或者PI或IR中确实没有记录

修复该错误,以便返回行

下一个问题对于向您运送最多特定商品的供应商,查询是否返回1 in rown列?

如果不是,则记录有些奇怪-看一下。如果是这样,我们都很好

一次又一次加入其他表:

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

继续前进,直到您突然丢失了原本不希望消失的记录...

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

也许“出售的物品”中的所有物品和“收到的物品”中的所有物品都不匹配! (也许您只出售了您没有更换过的东西,或者出售了您没有收到/从未出售的物品/已经出售但从未收到的物品中/现有库存的任何购买记录的物品

答案 1 :(得分:1)

您可以简单地与子查询一起加入

 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