建表:
create table test_rank(
name string,
subject string,
score string
)
;
插入數(shù)據(jù):
insert overwrite table test_rank values
('孫悟空','語(yǔ)文','87'),
('孫悟空','數(shù)學(xué)','95'),
('孫悟空','英語(yǔ)','68'),
('沙悟凈','語(yǔ)文','94'),
('沙悟凈','數(shù)學(xué)','56'),
('沙悟凈','英語(yǔ)','78'),
('宋松松','語(yǔ)文','64'),
('宋松松','數(shù)學(xué)','86'),
('宋松松','英語(yǔ)','84'),
('羅婷婷','語(yǔ)文','87'),
('羅婷婷','數(shù)學(xué)','85'),
('羅婷婷','英語(yǔ)','78')
;
執(zhí)行排序代碼:
select * ,
RANK() over(partition by subject order by score desc) rank,
DENSE_RANK() over(partition by subject order by score desc) dense_rank ,
ROW_NUMBER() over(partition by subject order by score desc) row_num
from test_rank
;
輸出結(jié)果:
全國(guó)排名TOP5的大學(xué)有20所么夫,我懷疑就是用dense_rank() 計(jì)算的者冤。。
另外档痪,不常用的排序函數(shù)還有:percent_rank()涉枫,ntile()。