0

我想选择一个分区的最小 ID。有点像 dense_rank 的工作方式我想要一个与每个分区关联的唯一编号,但我希望该编号成为分区中的最小主键。这是我所追求的一个例子。

USE AdventureWorks2012;  
GO  
SELECT '276 or 289 depending on what partition' as 'Min_BusinessEntityID'
  ,s.BusinessEntityID
  ,p.LastName
  ,s.SalesYTD
  ,a.PostalCode
 FROM Sales.SalesPerson AS s   
INNER JOIN Person.Person AS p   
    ON s.BusinessEntityID = p.BusinessEntityID  
INNER JOIN Person.Address AS a   
    ON a.AddressID = p.BusinessEntityID  
WHERE TerritoryID IS NOT NULL   
AND SalesYTD <> 0  
ORDER BY PostalCode, SalesYTD DESC

结果集示例

4

1 回答 1

1

SQL Server MIN命令接受 OVER() 子句:

SELECT MIN(s.BusinessEntityID) OVER (PARTITION BY TerritoryID) AS Min_BusinessEntityID
FROM ...
于 2017-11-06T18:29:53.197 回答