我有4个列的表:dept_no, emp_no, from_date, to_date
,其中每个dept_no
,emp_no
是经理。
我想找到当前使用的经理to_date
,并且要显示dept_no
和emp_no
。
样本数据:
|emp_no |dept_no | from_date | to_date |
| 11 | d001 | 1985-01-01 | 1991-10-01 |
| 12 | d001 | 1991-10-01 | 9999-01-01 |
| 21 | d002 | 1985-01-01 | 1989-12-17 |
| 22 | d002 | 1989-12-17 | 9999-01-01 |
| 31 | d003 | 1985-01-01 | 1992-03-21 |
| 32 | d003 | 1992-03-21 | 9999-01-01 |
样本输出:
|emp_no |dept_no |
|12 |d001 |
|22 |d002 |
|32 |d003 |
我发现了这一点:
SELECT dept_no
, emp_no
FROM
( SELECT dept_no
, MAX(to_date) as cur
FROM dept_manager
GROUP
BY dept_no) as new
JOIN dept_manager using(dept_no)
where cur = to_date;
我找到MAX(to_date)
每个部门的,然后在WHERE
子句中使用它。
这可行,但我认为应该有更好的方法来做到这一点。
我已经看到了许多类似的问题,但是没有一个问题对我有所帮助,因为我想显示不能在分组依据中使用的列。
一种可移植且通常有效的方法是使用子查询进行过滤:
select dept_no, emp_no
from dept_manager d
where to_date = (select max(d1.to_date) from dept_manager d1 where d1.dept_no = d.dept_no)
为了提高此查询的性能,您需要在上建立索引(dept_no, to_date)
。
另一个常用方法是窗口函数:
select *
from (
select d.*, row_number() over(partition by dept_no order by to_date desc) rn
from dept_manager d
) d
where rn = 1
根据您的数据库和版本,可能会有更整洁的选择。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句