我想选择一个分区的最小 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