Sql “具有最大日期的行中的属性”的分析函数
我正在重构一位同事的代码,我有几次他使用光标获取与某个谓词匹配的最新行: 他的技巧是将连接作为游标写入,按日期字段降序排列,打开游标,获取第一行,然后关闭游标 这需要为驱动此操作的结果集的每一行调用一个游标,这对于许多行来说代价很高。我更希望能够加入,但还有什么比关联子查询更便宜的呢Sql “具有最大日期的行中的属性”的分析函数,sql,oracle,analytic-functions,Sql,Oracle,Analytic Functions,我正在重构一位同事的代码,我有几次他使用光标获取与某个谓词匹配的最新行: 他的技巧是将连接作为游标写入,按日期字段降序排列,打开游标,获取第一行,然后关闭游标 这需要为驱动此操作的结果集的每一行调用一个游标,这对于许多行来说代价很高。我更希望能够加入,但还有什么比关联子查询更便宜的呢 select a.id_shared_by_several_rows, a.foo from audit_trail a where a.entry_date = (select max(a.entry_date)
select a.id_shared_by_several_rows, a.foo from audit_trail a
where a.entry_date = (select max(a.entry_date)
from audit_trail b
where b.id_shared_by_several_rows = a.id_shared_by_several_rows
);
我猜,由于这是一种常见的需求,所以有一个Oracle分析函数可以实现这一点。有分析等级、密集等级和行编号,用于根据排序标准识别行的序列号。它们在处理顺序列中没有差异的行的方式上有所不同。[例如你可以得到1,1,3或1,1,2或1,2,3。]
select index_name, column_name, column_position,
rank() over (partition by table_name order by column_position) rnk,
dense_rank() over (partition by table_name order by column_position) drnk,
row_number() over (partition by table_name order by column_position) rn
from all_ind_columns
where index_owner = 'SYSMAN'
and table_name = 'MGMT_JOB_EXECUTION';
因为分析是在选定的行上进行的,所以您仍然需要一个子查询/内联视图来过滤掉不需要的行。在本例中,INDEX_NAME是共享标识符
select index_name, column_name
from
(select index_name, column_name, column_position,
row_number() over (partition by index_name order by column_position) rn
from all_ind_columns
where index_owner = 'SYSMAN'
and table_name = 'MGMT_JOB_EXECUTION')
where rn = 1;
有分析等级、密集等级和行编号,用于根据排序标准识别行的序列号。它们在处理顺序列中没有差异的行的方式上有所不同。[例如你可以得到1,1,3或1,1,2或1,2,3。]
select index_name, column_name, column_position,
rank() over (partition by table_name order by column_position) rnk,
dense_rank() over (partition by table_name order by column_position) drnk,
row_number() over (partition by table_name order by column_position) rn
from all_ind_columns
where index_owner = 'SYSMAN'
and table_name = 'MGMT_JOB_EXECUTION';
因为分析是在选定的行上进行的,所以您仍然需要一个子查询/内联视图来过滤掉不需要的行。在本例中,INDEX_NAME是共享标识符
select index_name, column_name
from
(select index_name, column_name, column_position,
row_number() over (partition by index_name order by column_position) rn
from all_ind_columns
where index_owner = 'SYSMAN'
and table_name = 'MGMT_JOB_EXECUTION')
where rn = 1;
这将只对数据进行一次传递,并可用于根据需要从表中获取任意多的列,而无需进行自连接
select DISTINCT
a.id_shared_by_several_rows,
FIRST_VALUE(a.foo)
OVER (PARTITION BY a.id_shared_by_several_rows
ORDER BY a.entry_date DESC)
AS foo
from audit_trail a;
这将只对数据进行一次传递,并可用于根据需要从表中获取任意多的列,而无需进行自连接
select DISTINCT
a.id_shared_by_several_rows,
FIRST_VALUE(a.foo)
OVER (PARTITION BY a.id_shared_by_several_rows
ORDER BY a.entry_date DESC)
AS foo
from audit_trail a;
试试这个:
select id_shared_by_several_rows, foo from (
select a.id_shared_by_several_rows, a.foo, a.entry_date, max(a.entry_date) over (partition by a.id_shared_by_several_rows) max_entry_date
from audit_trail_a
) where entry_date = max_entry_date
试试这个:
select id_shared_by_several_rows, foo from (
select a.id_shared_by_several_rows, a.foo, a.entry_date, max(a.entry_date) over (partition by a.id_shared_by_several_rows) max_entry_date
from audit_trail_a
) where entry_date = max_entry_date
我相信你想用
select
max(id_shared_by_several_rows) keep (dense_rank first order by entry_date),
max(foo ) keep (dense_rank first order by entry_date)
from
audit_trail;
我相信你想用
select
max(id_shared_by_several_rows) keep (dense_rank first order by entry_date),
max(foo ) keep (dense_rank first order by entry_date)
from
audit_trail;