如何找到每个地区的最大订购量的产品?

时间:2019-04-11 09:37:02

标签: sql sql-server database

我需要找到每个区域中最大订购量的产品。结果应该是这样的:

| =IF(TEXT(TODAY(),"ddd")=IFERROR(VLOOKUP(A1, {{"M","Mon"};{"T","Tue"};{"W","Wed"};{"TH","Thu"};{"F","Fri"};{"S","Sat"};{"SU","Sun"}}, 2,0)),1) |

这是数据库:

enter image description here

P.S到数据库备份的链接: https://drive.google.com/open?id=15iE53xxni9C7uGaaa-kcK4b6WnmQv_1f

这是我尝试的代码,但它仅针对一个地区进行计算。我需要针对所有区域进行动态计算。

RegionID | ProductID

4 个答案:

答案 0 :(得分:0)

您可以使用该分区来实现此目的,我已根据您的图表加入了必要的表。

        SELECT REGIONID,PRODUCTID
        FROM(SELECT *,Row_number() over(partition by REGIONID order by Total desc) rn
        FROM(Select T.REGIONID,OD.PRODUCTID,COUNT(OD.PRODUCTID) as Total
        From Orders O
        LEFT JOIN EmployeeTerritories ET
        on O.EmployeeID=ET.EmployeeID
        LEFT JOIN Territories T
        on T.TerritoryID=ET.TerritoryID
        LEFT JOIN OrderDetails OD
        on OD.OrderID=O.OrderID
        GROUP BY T.REGIONID,OD.PRODUCTID)a)b
    WHERE b.rn=1

由于您想要一个不带分区的替代产品,因此可以有一个子查询,以`获得每个区域的最大产品销售量,并使用该子查询加入以获得每个区域的最大销售产品。

SELECT a.RegionID,a.PRODUCTID
(Select T.REGIONID,OD.PRODUCTID,COUNT(OD.PRODUCTID) as Total
    From Orders O
    LEFT JOIN EmployeeTerritories ET
    on O.EmployeeID=ET.EmployeeID
    LEFT JOIN Territories T
    on T.TerritoryID=ET.TerritoryID
    LEFT JOIN OrderDetails OD
    on OD.OrderID=O.OrderID
    GROUP BY T.REGIONID,OD.PRODUCTID)a
INNER JOIN
(Select RegionID,max(Total) as MaXTotal
FROM(Select T.REGIONID,OD.PRODUCTID,COUNT(OD.PRODUCTID) as Total
    From Orders O
    LEFT JOIN EmployeeTerritories ET
    on O.EmployeeID=ET.EmployeeID
    LEFT JOIN Territories T
    on T.TerritoryID=ET.TerritoryID
    LEFT JOIN OrderDetails OD
    on OD.OrderID=O.OrderID
    GROUP BY T.REGIONID,OD.PRODUCTID)b
GROUP BY RegionID)c
on c.RegionID=a.RegionID and a.Total=c.MaxTotal

答案 1 :(得分:0)

join再次查询您的地区表。

select * 
from  Region
left join(
SELECT   T.RegionID, T.ProductID, MAX(T.Quantity) AS MaxOrders
FROM(
    SELECT Region.RegionID, [Order Details].ProductID, SUM([Order Details].Quantity) AS Quantity
    FROM Region
    JOIN Territories ON Region.RegionID = Territories.RegionID
    JOIN EmployeeTerritories ON Territories.TerritoryID = EmployeeTerritories.TerritoryID
    JOIN Employees ON EmployeeTerritories.EmployeeID = Employees.EmployeeID
    JOIN Orders ON Employees.EmployeeID = Orders.EmployeeID
    JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
    GROUP BY Region.RegionID, [Order Details].ProductID
) T
--WHERE T.RegionID = 1
GROUP BY T.RegionID, T.ProductID
 ) A on A.RegionID =  Region.RegionID

答案 2 :(得分:0)

交叉申请应该对此有所帮助。

conda list --version

答案 3 :(得分:0)

您可以在聚合查询中使用窗口函数。看起来像:

SELECT RegionID, ProductID, Quantity AS MaxOrders
FROM (SELECT t.RegionID, od.ProductID, 
             SUM(od.Quantity) AS Quantity,
             ROW_NUMBER() OVER (PARTITION BY t.RegionID ORDER BY SUM(od.Quantity) DESC) as seqnum
      FROM Territories t JOIN
           EmployeeTerritories et
           ON t.TerritoryID = et.TerritoryID JOIN
           Employees e
           ON et.EmployeeID = e.EmployeeID JOIN
           Orders o
           ON e.EmployeeID = o.EmployeeID JOIN
           [Order Details] od
           ON o.OrderID = od.OrderID
      GROUP BY t.RegionID, od.ProductID
     ) t
WHERE seqnum = 1
ORDER BY MaxOrders DESC;

注意:

  • 表别名使查询更易于编写和阅读。
  • Region表是不必要的,因为您需要的列在Territories中。
  • 外部查询不需要聚合,只需过滤即可。