题解 | 给出employees表中排名为奇数行的first_name

给出employees表中排名为奇数行的first_name

https://www.nowcoder.com/practice/e3cf1171f6cc426bac85fd4ffa786594

解题思路一:

因为employees 是按照emp_no升序排序,所以后续我也按照emp_no升序排序保证rang大的仍然在前面显示。 我认为这种操作是站得住脚的,原表一定是按照某种顺序排列,后续才有必要继续按照这种顺序排列。

with ranking as (select 
    emp_no,
    first_name,
    rank() over(
        order by first_name asc
    ) as rk
from employees)
select
    first_name as first
from ranking 
where rk%2=1
order by emp_no

解题思路二:

如果抛开上述逻辑,我的写法是

select
    e.first_name as first
from employees e
join (
    select
        first_name,
        rank() over(order by first_name asc) as rk
    from employees
) t
on e.first_name = t.first_name
and t.rk%2=1

全部评论

相关推荐

不知道怎么取名字_:看来现在卷的,这种单位都开始提高要求了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务