Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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 Server仅从总匹配中返回1行_Sql_Sql Server_Min_Having - Fatal编程技术网

SQL Server仅从总匹配中返回1行

SQL Server仅从总匹配中返回1行,sql,sql-server,min,having,Sql,Sql Server,Min,Having,我做了一个查询,将我们仓库中产品的产品尺寸与我们新库存位置的尺寸进行匹配。输出示例如下: 示例:产品0012953目前包装在“MB”中,但在新仓库中它可以装入33、E2、E3、KK和C4。如果它可以在多个地点,它应该进入一个最低的排名。在这种情况下,它应该转到排名为10的NewPackaging'33' 产品0066487应该在排名第13位的KK 如何调整查询,使其只返回每个ArticleNr排名最低的记录 期望输出: ArticleNr Location Packaging St

我做了一个查询,将我们仓库中产品的产品尺寸与我们新库存位置的尺寸进行匹配。输出示例如下:

示例:产品0012953目前包装在“MB”中,但在新仓库中它可以装入33、E2、E3、KK和C4。如果它可以在多个地点,它应该进入一个最低的排名。在这种情况下,它应该转到排名为10的NewPackaging'33'

产品0066487应该在排名第13位的KK

如何调整查询,使其只返回每个ArticleNr排名最低的记录

期望输出:

ArticleNr   Location    Packaging   StockAtLocation NewPackaging    Ranking

0012953     A15074E03       MB           235            33            10
0066487     A20057A03       KK           12             KK            13
查询:

SELECT
  t1.ArticleNr,
  t1.Location AS Location,
  t1.StorageMedium AS Packaging,
  t1.StockAtLocation,
  t2.Verpakking AS NewPackaging,
  t2.Ranking
FROM #Info t1,
     #Dimensions t2
WHERE t1.Length < t2.Lengte 
AND t1.Width < t2.Breedte 
AND t1.Height < t2.Hoogte 
AND t1.Volume < t2.MaxVol 
AND (t1.PartWeightGross / 1000) < t2.MaxWeightArt 
AND (t1.Volume * t1.StockAtLocation) < t2.MaxVol 
AND ((t1.PartWeightGross / 1000) * t1.StockAtLocation) < t2.MaxWeightEmb 
Order by ArticleNr asc, Ranking asc

将行号添加到Select并选择其中行号=1

;with cte
as
(
SELECT
RN = ROW_NUMBER() OVER(PARTITION BY t1.ArticleNr ORDER BY ArticleNr asc, Ranking asc),
  t1.ArticleNr,
  t1.Location AS Location,
  t1.StorageMedium AS Packaging,
  t1.StockAtLocation,
  t2.Verpakking AS NewPackaging,
  t2.Ranking
FROM #Info t1,
     #Dimensions t2
WHERE t1.Length < t2.Lengte 
AND t1.Width < t2.Breedte 
AND t1.Height < t2.Hoogte 
AND t1.Volume < t2.MaxVol 
AND (t1.PartWeightGross / 1000) < t2.MaxWeightArt 
AND (t1.Volume * t1.StockAtLocation) < t2.MaxVol 
AND ((t1.PartWeightGross / 1000) * t1.StockAtLocation) < t2.MaxWeightEmb 
)
select
    *
    from cte
        where rn = 1

谢谢,这很好用,我相信我以后还会用的
;with cte
as
(
SELECT
RN = ROW_NUMBER() OVER(PARTITION BY t1.ArticleNr ORDER BY ArticleNr asc, Ranking asc),
  t1.ArticleNr,
  t1.Location AS Location,
  t1.StorageMedium AS Packaging,
  t1.StockAtLocation,
  t2.Verpakking AS NewPackaging,
  t2.Ranking
FROM #Info t1,
     #Dimensions t2
WHERE t1.Length < t2.Lengte 
AND t1.Width < t2.Breedte 
AND t1.Height < t2.Hoogte 
AND t1.Volume < t2.MaxVol 
AND (t1.PartWeightGross / 1000) < t2.MaxWeightArt 
AND (t1.Volume * t1.StockAtLocation) < t2.MaxVol 
AND ((t1.PartWeightGross / 1000) * t1.StockAtLocation) < t2.MaxWeightEmb 
)
select
    *
    from cte
        where rn = 1