sql MS SQL数据聚合 - NTILE

Posted

tags:

篇首语:本文由小常识网(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的主要内容,如果未能解决你的问题,请参考以下文章

Apache Spark 使用 SQL 函数 nTile 对数据进行分区

sql MS SQL数据聚合 - RANK和DENSE_RANK

PCB MS SQL CLR聚合函数(函数作用,调用顺序,调用次数) CLR说明

开窗函数

sql MS SQL简单聚合

MS SQL 中的非聚合字段