(3.6)查询前 20% 时间的订单信息
select name,
orderdate,
cost,
ntile(5) over(order by orderdate) sorted
from business;
结果显示:
说明:ntile(n)函数会将数据等分成 n 份。
select * from (
select name,
orderdate,
cost,
ntile(5) over(order by orderdate) sorted
from business ) t
where sorted = 1;
结果显示:
4 )Rank(窗口函数)
1)函数说明:就是对数据进行排序,注意要跟 over()一起用才行,并且 Over()函数必须跟着分区或排序条件。
RANK() :排序相同时会重复,总数不会变,如:1-2-3-3-5-6
DENSE_RANK() :排序相同时会重复,总数会减少,如:1-2-3-3-4-5
ROW_NUMBER() :会根据顺序计算,如:1-2-3-4-5
例:结果显示的是 rank()函数
select
name,
subject,
score,
rank() over(partition by subject order by score desc) rp,
dense_rank() over(partition by subject order by score desc) drp,
row_number() over(partition by subject order by score desc) rmp
from score
Where rp < 4;