dense_rank() 窗口函数:返回的是结果集分区每行的排名,排名值没有间断,是连续的排名值
栗子:
sales_employee fiscal_year sale
Alice 2016 150.00
Alice 2017 100.00
Alice 2018 200.00
Bob 2016 100.00
Bob 2017 150.00
Bob 2018 200.00
John 2016 200.00
John 2017 150.00
John 2018 250.00
SQL:
SELECT
sales_employee,fiscal_year,sale,
DENSE_RANK() OVER (PARTITION BY fiscal_year ORDER BY sale DESC) AS sales_rank
FROM
sales;
输出如下:
sales_employee fiscal_year sale sales_rank
John 2016 200.00 1
Alice 2016 150.00 2
Bob 2016 100.00 3
Bob 2017 150.00 1
John 2017 150.00 1
Alice 2017 100.00 2
John 2018 250.00 1
Alice 2018 200.00 2
Bob 2018 200.00 2
这个栗子用DENSE_RANK() 函数对结果集的每个分区的行进行排名
语法
DENSE_RANK() OVER(
PARTITION BY [col1]
ORDER BY [col2] DESC
)
PARTITION BY按年分区
ORDER BY按销售额降序
DENSE_RANK()对分区内记录按指定条件排名