代码杂谈-SQL中的rank&row_number函数教程
两个函数细节记不住. 写个例子备注一下.
<pre class="sql">```
select
no, name, score
, rank() over(partition by no order by score asc) rk1
, rank() over(partition by no order by score desc) rk2
, row_number() over(partition by no order by score asc) rn1
, row_number() over(partition by no order by score desc) rn2
from values
(1,'a',1), (1,'a', null),
(1, 'b',2), (1, 'b',-2), (1, 'b',1)
t (no, name, score)
;
结果
nonamescorerk1rk2rn1rn21b251511a132321b132431b-224241a\N1515