我们如何删除由 LEAD 函数创建的列
How do we remove the column that is created by LEAD function
当我运行运行下面查询
SELECT
*
FROM
(
SELECT
LEAD(hire_date)
OVER(PARTITION BY department_id
ORDER BY
hire_date
) AS recent_joinee,
a.*
FROM
employees a
)
WHERE
recent_joinee IS NULL;
我得到的结果低于结果
但我不需要第一列“Recent_joinee”,因为这是由引导函数创建的,其余的“n" 我需要的列数。
对于这种情况,我需要做什么?
您应该指定所需的列。
您可以获取所有字段 (*) 或指定您想要的字段。
列出所有列可能很麻烦。 Oracle 没有删除列的简单方法。对于您的示例,您可能会产生额外连接的开销(如果键是主键,这不会太多):
SELECT e.*
FROM employees e JOIN
(SELECT LEAD(hire_date) OVER (PARTITION BY department_id
ORDER BY hire_date
) AS recent_joinee,
e2.*
FROM employees e2
) e2
USING (employee_id)
WHERE e2.recent_joinee IS NULL;
或者,您可以使用相关子查询:
select e.*
from employee e
where not exists (select 1
from employee e2
where e2.department_id = e.department_id and
e2.hire_date > e.hire_date
);