选择带有内部联接的大多数出现的值SQL

时间:2019-04-22 13:24:22

标签: sql sql-server

我正在使用此查询从不同的链接表中获取以下数据。但是,假设某项的VENDORS为3。现在在这里,我想向大家展示发生次数最多的供应商。我的意思是如果项目ABC由3个不同的供应商多次提供。然后在这里我想让供应商提供大部分时间的项目ABC。

我的查询是这个。

use iBusinessFlex;
SELECT Items.Name, 
Max(Items.ItemID) as ItemID ,
MAX(Items.Description)as Description,
MAX(ItemsStock.CurrentPrice) as UnitPrice,
MAX(ItemsStock.Quantity) as StockQuantiity,
MAX(Vendors.VendorName) as VendorName, 
SUM(ItemReceived.Quantity) as TotalQuantity
From ItemReceived  
INNER JOIN Items ON ItemReceived.ItemId=Items.ItemID 
INNER JOIN ItemsStock ON ItemReceived.ItemId=ItemsStock.ItemID 
INNER JOIN PurchaseInvoices ON PurchaseInvoices.PurchaseInvoiceId = ItemReceived.PurchaseInvoiceId 
INNER JOIN Vendors ON Vendors.VendorId = PurchaseInvoices.VendorId
Group By Items.Name  

编辑:我已经包含了这个子查询,但是我不确定它是否显示正确的结果。我的意思是为大多数时间提供该商品的每个商品显示供应商

use iBusinessFlex;
SELECT Items.Name,
Max(Items.ItemID) as ItemID ,
MAX(Items.Description)as Description,MAX(ItemsStock.CurrentPrice) as UnitPrice,
MAX(ItemsStock.Quantity) as StockQuantiity,MAX(Vendors.VendorName) as VendorName, 
SUM(ItemReceived.Quantity) as TotalQuantity
From ItemReceived  
INNER JOIN Items ON ItemReceived.ItemId=Items.ItemID INNER JOIN ItemsStock 
ON ItemReceived.ItemId=ItemsStock.ItemID INNER JOIN PurchaseInvoices 
ON PurchaseInvoices.PurchaseInvoiceId = ItemReceived.PurchaseInvoiceId INNER JOIN Vendors
ON Vendors.VendorId IN (
SELECT Top 1 MAX(PurchaseInvoices.VendorId) as VendorOccur
FROM PurchaseInvoices INNER JOIN Vendors ON Vendors.VendorId=PurchaseInvoices.VendorId
GROUP BY PurchaseInvoices.VendorId
ORDER BY COUNT(*) DESC

结果看起来像这样。
Result of this query Image

VendorsTable

1 个答案:

答案 0 :(得分:1)

首先,我将从谁订购最多的东西开始。但是,MOST的依据是……数量最多?价格?,次数?如果您使用一个供应商并订购10的数量的6倍,则您有60件商品。但是从另一家供应商处订购了1次,数量为100数量,以中标。您必须决定MOST的基础,但是我会根据大多数时间 根据您原来的问题。

所以所有东西都来自具有供应商ID的PurchasedInvoices。我不在乎供应商是谁,只是他们的ID,所以不需要加入。另外,如果我只是在查询数量,就不需要项目名称。下面的查询将显示每个项目,每个供应商及其各自的最多订购时间和订购数量。我添加了项目,并且供应商表联接只是为了显示名称。

select
        IR.ItemID,
        PI.VendorID,
        max( I.Name ) Name,
        max( V.VendorName ) VendorName,
        count(*) as TimesOrderedFrom,
        SUM( IR.Quantity ) as QuantityFromVendor
    from
        ItemsReceived IR
            JOIN PurchaseInvoices PI
                on IR.PurchaseInvoiceID = PI.PurchaseInvoiceID
            JOIN Items I
                on IR.ItemID = I.ItemID
            JOIN Vendors V
                on IR.VendorID = V.VendorID
    group by
        IR.ItemID,
        PI.VendorID
    order by
        -- Per item
        IR.ItemID,
        -- Most count ordered
        count(*),
        -- If multiple vendors, same count, get total quantity
        sum( IR.Quantity )

现在,每个项目只获得1个,这将创建一个相关的子查询,您 可以添加“ TOP 1”以仅返回第一个。由于总数 已经完成,则可以获取供应商联系信息。

select
        I.Name,
        V.VendorName,
        TopVendor.TimesOrderedFromVendor,
        TopVendor.QuantityFromVendor
    from
        Items I
            JOIN ( select TOP 1
                            IR.ItemID,
                            PI.VendorID,
                            count(*) as TimesOrderedFrom,
                            SUM( IR.Quantity ) as QuantityFromVendor
                        from
                            ItemsReceived IR
                                JOIN PurchaseInvoices PI
                                    on IR.PurchaseInvoiceID = PI.PurchaseInvoiceID
                        where
                            -- correlated subquery based on the outer-most item
                            IR.ItemID = I.ItemID
                        group by
                            IR.ItemID,
                            PI.VendorID
                        order by
                            -- Per item
                            IR.ItemID,
                            -- Most count ordered
                            count(*),
                            -- If multiple vendors, same count, get total quantity
                            sum( IR.Quantity ) ) TopVendor
                on I.ItemID = TopVendor.ItemID
                JOIN Vendors V
                    on TopVendor.VendorID = V.VendorID

让INNER子查询加入供应商和仅用于名称的项目是没有意义的。一旦选择了顶级供应商,就只能获得一次。