select emp_no, birth_date, first_name, last_name, gender, hire_date from ( select emp_no, birth_date, first_name, last_name, gender, hire_date, rank()over(order by hire_date desc ) posn from employees ) as t where t.posn = 1
1、查找,最晚,早,小,大,一般都需要用到“窗口函数”, 而且需要对窗口函数进行赋值排序号处理,这个种情况下就要用到“子查询” 。 单一行的子查询,使用在 where 语句中, 多行多列(形成另一张表)的子查询,就必须要使用在 from语句中。
(注: from语句 嵌套 子查询,一定要 进行 别名。 as t)
2、排序“窗口函数”的使用,考虑到 最晚的可能有重复值,即不是唯一,是可以并列的,所以使用 rank()over() 函数。
(row_number()over() 是连续不并列的。dense_rank()over() 是连续且并列的,一般特殊情况下才用此函数。常用的还是 rank 和 row_number )
3、最后,把位置条件 即: t.posn=1的数据 取出 即可以。