分组依据和子查询

时间:2018-02-21 18:58:36

标签: sql sql-server

我正在学习Northwind数据库。我发现使用此查询在每个地区销售的产品有多少次:

     select R.RegionID
                  , R.RegionDescription
                  , P.ProductID
                  , P.ProductName
                  , sum(OD.Quantity) as SoldQuantity
    from Region as R inner join Territories as T on R.RegionID = T.RegionID
                     inner join EmployeeTerritories as ET on T.TerritoryID = ET.TerritoryID
                     inner join Employees as E on ET.EmployeeID = E.EmployeeID
                     inner join Orders as O on E.EmployeeID = O.EmployeeID
                     inner join [Order Details] as OD on O.OrderID = OD.OrderID
                     inner join Products as P on OD.ProductID = P.ProductID
    group by R.RegionID, R.RegionDescription, P.ProductID, P.ProductName

但我很难为每个地区提取最大'SoldQuantity'(带有ProductID和ProductName)的那些。

我设法通过此查询找到每个地区的'MaxSoldQuantity':

    select NESTED.RegionID
         , NESTED.RegionDescription
         , max(NESTED.SoldQuantity) as MaxSoldQuantity
    from (
        select R.RegionID as RegionID
                    , R.RegionDescription as RegionDescription
                    , P.ProductID as ProductID
                    , P.ProductName as ProductName
                    , sum(OD.Quantity) as SoldQuantity
        from Region as R inner join Territories as T on R.RegionID = T.RegionID
                         inner join EmployeeTerritories as ET on T.TerritoryID = ET.TerritoryID
                         inner join Employees as E on ET.EmployeeID = E.EmployeeID
                         inner join Orders as O on E.EmployeeID = O.EmployeeID
                         inner join [Order Details] as OD on O.OrderID = OD.OrderID
                         inner join Products as P on OD.ProductID = P.ProductID
        group by R.RegionID, R.RegionDescription, P.ProductID, P.ProductName
    ) as NESTED
    group by NESTED.RegionID, NESTED.RegionDescription

但每当我添加'NESTED.ProductID'和'NESTED.ProductName'来选择和分组时,我得到的结果与第一个查询相同。

所以我的问题是:如何获得每个地区销售量最大的产品数据?

2 个答案:

答案 0 :(得分:2)

您不提供任何样本数据,因此我无法对其进行测试。但我认为您可以使用窗口函数对Quantity进行排名。最外面的查询获取最大值

select *
from (
    select *
        , row_number over (partition by RegionID order by Quantity desc) as row_num
    from (
        select R.RegionID as RegionID
            , R.RegionDescription as RegionDescription
            , P.ProductID as ProductID
            , P.ProductName as ProductName
            , sum(OD.Quantity) as SoldQuantity
        from Region as R 
        inner join Territories as T on R.RegionID = T.RegionID
        inner join EmployeeTerritories as ET on T.TerritoryID = ET.TerritoryID
        inner join Employees as E on ET.EmployeeID = E.EmployeeID
        inner join Orders as O on E.EmployeeID = O.EmployeeID
        inner join [Order Details] as OD on O.OrderID = OD.OrderID
        inner join Products as P on OD.ProductID = P.ProductID
        group by R.RegionID, R.RegionDescription, P.ProductID, P.ProductName
    ) nested
) ranked
where row_num = 1

答案 1 :(得分:2)

您应该加入嵌套查询以获取ProductName和ProductID

DataInputStream in = new DataInputStream(server.getInputStream());
String msg = in.readUTF();
System.out.println(msg);