Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 分组依据和子查询_Sql_Sql Server - Fatal编程技术网

Sql 分组依据和子查询

Sql 分组依据和子查询,sql,sql-server,Sql,Sql Server,我在北风数据库学习。通过此查询,我发现每个产品在每个地区的销售次数: select R.RegionID , R.RegionDescription , P.ProductID , P.ProductName , sum(OD.Quantity) as SoldQuantity from Region as R inner join T

我在北风数据库学习。通过此查询,我发现每个产品在每个地区的销售次数:

     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
    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
但我很难用ProductID和ProductName为每个地区提取具有最大“SoldQuantity”的产品

我通过此查询找到了每个地区的“MaxSoldQuantity”:

     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
    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”以进行选择和分组时,我都会得到与第一次查询相同的结果


所以我的问题是:我如何才能获得每个地区哪种产品最畅销的数据?

您没有提供任何样本数据,因此我无法对其进行测试。但我认为你可以使用窗口函数来对数量进行排序。最外层的查询获取最大值

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

您应该加入对ProductName和ProductID的嵌套查询

   select T1.RegionID
        , T1.RegionDescription 
        , T1.MaxSoldQuantity
        , N2.ProductName
        , N2.ProductID
   from (
      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 ) T1
  inner join (
     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
  ) N2 on N2.SoldQuantity = T1.MaxSoldQuantity 

但在最后一行上方无法访问“MaxSoldQuantity”