篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MS SQL数据聚合 - NTILE相关的知识,希望对你有一定的参考价值。
-- Dividing rows into 2 groups
SELECT p.FirstName, p.LastName
,NTILE(2) OVER(ORDER BY SalesYTD DESC) AS Quartile
, 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 SalesYTD <> 0;
-- Partition by Postal Code
-- And then divided into four groups within
-- each PostalCode.
SELECT p.FirstName, p.LastName
,NTILE(4) OVER(PARTITION BY PostalCode
ORDER BY SalesYTD DESC) AS Quartile
,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;
以上是关于sql MS SQL数据聚合 - NTILE的主要内容,如果未能解决你的问题,请参考以下文章