不同表中的SQL COUNT

时间:2017-11-01 14:29:08

标签: sql sql-server tsql adventureworks

对于每个供应商,我想总结 - 供应商销售的多种产品 - 订单数量(PurchaseOrderID) -order值(SubTotal)

这就是我的数据库的样子

enter image description here

这就是我现在的地方

class Spam:
    def __init__(self):
        from collections import defaultdict
        self.eggs = defaultdict(list)
    def __getitem__(self, index):
        class AdderHelper:
            def __init__(self, eggs):
                self.eggs = eggs
            def __iadd__(self, egg):
                self.eggs.append(egg)
                return self
        return AdderHelper(self.eggs[index])

结果不是我想要的。我希望计数适用于每个供应商。

由于

1 个答案:

答案 0 :(得分:0)

我认为问题的一部分是1:M关系导致人为膨胀的计数。要解决此问题,您可以使用派生表或分析函数。 使用派生表:

SELECT pv.Name, 
Continent = CASE pc.Name
WHEN 'United States' THEN 'Nordamerika'
WHEN 'England' THEN 'Europa'
WHEN 'Germany' THEN 'Europa'
WHEN 'France' THEN 'Europa'
END,
CntProd NumberOfProducts,
cntPO NumberOfOrders 
FROM Purchasing.Vendor pv
INNER JOIN Person.BusinessEntity pbe ON pv.BusinessEntityID = pbe.BusinessEntityID
INNER JOIN Person.BusinessEntityAddress pbea ON pbe.BusinessEntityID = pbea.BusinessEntityID
INNER JOIN Person.Address pa ON pbea.AddressID = pa.AddressID
INNER JOIN Person.StateProvince psp ON pa.StateProvinceID = psp.StateProvinceID
INNER JOIN Person.CountryRegion pc ON psp.CountryRegionCode = pc.CountryRegionCode  
INNER JOIN (SELECT count(ProductID) CntProd, BusinessEntityID 
            FROM Purchasing.ProductVendor 
            GROUP BY BusinessEntityID) ppv ON pv.BusinessEntityID = ppv.BusinessEntityID
INNER JOIN (SELECT count(PurcahseOrderID) CntPO, VendorID
            FROM Purchasing.PurchaseOrderHeader 
            GROUP BY VendorID) ppoh ON pv.BusinessEntityID = ppoh.VendorID 

使用窗口函数(虽然我不确定这里的结果)给出了大陆与集团的案例陈述,依据供应商名称和供应商区域与大陆。可能需要通过以下方式调整组:

GROUP BY pv.Name, CASE pc.Name
                  WHEN 'United States' THEN 'Nordamerika'
                  WHEN 'England' THEN 'Europa'
                  WHEN 'Germany' THEN 'Europa'
                  WHEN 'France' THEN 'Europa'
                  END

SELECT pv.Name, 
Continent = CASE pc.Name
WHEN 'United States' THEN 'Nordamerika'
WHEN 'England' THEN 'Europa'
WHEN 'Germany' THEN 'Europa'
WHEN 'France' THEN 'Europa'
END,
COUNT(ProductID) over (partition by PPV.BusinessEntityID) NumberOfProducts,
COUNT(PurchaseOrderID) over (partition by ppoh.VendorID) NumberOfOrders 
FROM Purchasing.Vendor pv
INNER JOIN Person.BusinessEntity pbe ON pv.BusinessEntityID = pbe.BusinessEntityID
INNER JOIN Person.BusinessEntityAddress pbea ON pbe.BusinessEntityID = pbea.BusinessEntityID
INNER JOIN Person.Address pa ON pbea.AddressID = pa.AddressID
INNER JOIN Person.StateProvince psp ON pa.StateProvinceID = psp.StateProvinceID
INNER JOIN Person.CountryRegion pc ON psp.CountryRegionCode = pc.CountryRegionCode  
INNER JOIN Purchasing.ProductVendor ppv ON pv.BusinessEntityID = ppv.BusinessEntityID
INNER JOIN Purchasing.PurchaseOrderHeader ppoh ON pv.BusinessEntityID = ppoh.VendorID 
GROUP BY pv.Name, pc.Name