Oracle SQL - 如何获取具有最大日期的记录的分配ID

时间:2015-11-27 10:49:10

标签: sql oracle greatest-n-per-group

我试图获得该职位最后一名员工的分配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'

我将最大日期转换为字符,以便在结果中对其进行子串。

5 个答案:

答案 0 :(得分:3)

使用窗口函数可以很容易地解决这样的问题:

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_datemax(paaf.effective_start_date) ... group by position_id基本相同,但无需对整个结果进行分组。

由于您只选择一个position_id可以使用over ()代替 - 但是使用over (partition by position_id)该查询也可用于检索该信息多个职位。

答案 1 :(得分:0)

试试这个

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')

答案 2 :(得分:0)

只要我知道,你不能获得聚合函数返回的结果行的另一个值,你需要使用子查询。我认为你应该这样做:

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'
     )

答案 3 :(得分:0)

如果您只想要一个ID:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="rules">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>            
            <rule>
                <name>zz</name>
            </rule>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="rule[name='aa']"/>
    <xsl:template match="rule[name='bb']"/>
    <xsl:template match="rule[name='cc']"/>
</xsl:stylesheet>

如果您想要所有与最新日期相关联的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;

答案 4 :(得分:0)

这是一个替代方案,它不需要子查询或分析函数来查找您之后的值:

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');