Select 分区中的最小 ID - MSSQL t-SQL
Select Min ID in a partition - MSSQL t-SQL
我想select分区的最小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
Result Set Example
SQL 服务器 MIN 命令接受 OVER() 子句:
SELECT MIN(s.BusinessEntityID) OVER (PARTITION BY TerritoryID) AS Min_BusinessEntityID
FROM ...
我想select分区的最小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
Result Set Example
SQL 服务器 MIN 命令接受 OVER() 子句:
SELECT MIN(s.BusinessEntityID) OVER (PARTITION BY TerritoryID) AS Min_BusinessEntityID
FROM ...