我正在尝试获取该职位的最后一位雇员的工作分配ID。那将是拥有职位或最长日期的最后一个任务。如何在下面的查询中检索两者?
select max(to_char(paaf.effective_start_date, 'yyyymmdd')) || to_char(paaf.assignment_id)
from apps.per_all_assignments_f paaf
where paaf.position_id = 159841
and paaf.assignment_type in ('E', 'C')
and paaf.primary_flag = 'Y'
我将最大日期转换为一个字符,以便将其细分为结果。
使用window函数可以轻松解决此类问题:
select *
from (
select paaf.*,
max(paaf.effective_start_date) over (partition by position_id) as max_start_date
from apps.per_all_assignments_f paaf
where paaf.position_id = 159841
and paaf.assignment_type in ('E', 'C')
and paaf.primary_flag = 'Y'
) t
where effective_start_date = max_start_date;
该部分max(paaf.effective_start_date) over (partition by position_id) as max_start_date
基本上与a相同,max(paaf.effective_start_date) ... group by position_id
但无需对整个结果进行分组。
因为您只选择一个position_id
,所以可以使用over ()
-但是通过使用over (partition by position_id)
查询,也可以用于检索多个职位的信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句