sql rank diplay请求的排名号

时间:2018-09-24 22:25:54

标签: sql rank

查询:

SELECT distinct 
rank() OVER (ORDER BY p.service) as rank,
p.service, 
From table_service
Where 1=1

输出:
排名:1 |服务:test1
排名:2 |服务:test2
排名:3 |服务:test3

我如何仅显示2级?

我尝试了此方法,但没有用:

SELECT distinct 
rank() OVER (ORDER BY p.service) as rank,
p.service, 
From table_service
Where rank = 2

有什么主意吗?

3 个答案:

答案 0 :(得分:1)

您可以尝试使用子查询,因为where不能使用别名来做条件。

SELECT * FROM (
    SELECT 
        rank() OVER (ORDER BY p.service) as rank,
        p.service, 
    From table_service
) t1
where rank = 2

答案 1 :(得分:1)

将您的查询放入子查询中

select *
from
(
SELECT distinct 
rank() OVER (ORDER BY p.service) as rank,
p.service, 
From table_service
) sub
Where rank = 2;

答案 2 :(得分:1)

为什么使用等级?大多数数据库支持offset/fetch或类似的东西:

select p.service,
from table_service p
order by p.service
offset 1 fetch first 1 row only;