簡體   English   中英

Oracle SQL - 如何獲取具有最大日期的記錄的分配ID

[英]Oracle SQL - How to get the assignment id of the record with the maximum date

我正在嘗試獲得該職位最后一名員工的任務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'

我正在將最大日期轉換為字符,以便在結果中將其子串。

使用窗口函數可以輕松解決類似的問題:

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的部分max(paaf.effective_start_date) over (partition by position_id) as max_start_date基本上與max(paaf.effective_start_date) ... group by position_idmax(paaf.effective_start_date) ... group by position_id但不需要對整個結果進行分組。

由於您只選擇一個position_id可以使用over () - 但是通過使用over (partition by position_id) ,查詢也可以用於檢索多個位置的信息。

嘗試這個

select (paaf.assignment_id)
from   apps.per_all_assignments_f paaf
where  paaf.position_id = 159841 
and    paaf.primary_flag = 'Y'
UNION all
select (paaf.assignment_id)
from   apps.per_all_assignments_f paaf
where paaf.assignment_type in ('E', 'C')
and    paaf.primary_flag = 'Y'
AND paaf.effective_start_date = (select max(paaf.effective_start_date) from   apps.per_all_assignments_f paaf
where paaf.assignment_type in ('E', 'C')
and    paaf.primary_flag = 'Y')

只要我知道,你不能得到聚合函數返回的結果行的另一個值,你需要使用子查詢。 我認為你應該做的事情如下:

select to_char(paaf.effective_start_date, 'yyyymmdd') || to_char(paaf.assignment_id)
from apps.per_all_assignments_f paaf
where max(to_char(paaf.effective_start_date, 'yyyymmdd')) = 
     (
         select max(to_char(paaf2.effective_start_date, 'yyyymmdd'))
         from   apps.per_all_assignments_f paaf2
         where  paaf2.position_id = 159841 
         and paaf2.assignment_type in ('E', 'C')
         and paaf2.primary_flag = 'Y'
     )

如果您只想要一個id:

SELECT  to_char(effective_start_date, 'yyyymmdd') || to_char(assignment_id)
FROM (
  select effective_start_date,
         assignment_id
  from   apps.per_all_assignments_f
  where  position_id = 159841 
  and    assignment_type in ('E', 'C')
  and    primary_flag = 'Y'
  ORDER BY effective_start_date DESC
)
WHERE ROWNUM = 1;

如果您想要與最新日期關聯的所有ID,那么:

SELECT  to_char(effective_start_date, 'yyyymmdd') || to_char(assignment_id)
FROM (
  select effective_start_date,
         assignment_id,
         RANK() OVER ( ORDER BY effective_start_date DESC ) AS rnk
  from   apps.per_all_assignments_f
  where  position_id = 159841 
  and    assignment_type in ('E', 'C')
  and    primary_flag = 'Y'
  ORDER BY effective_start_date DESC
)
WHERE RNK = 1;

這是一個替代方案,它不需要子查詢或分析函數來查找您所追求的值:

select to_char(max_effective_start_date, 'yyyymmdd') || to_char(max_row_assignment_id)
from   (select max(paaf.effective_start_date) max_effective_start_date,
               max(paaf.assignment_id) keep (dense_rank first order by effective_start_date desc) max_row_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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM