Oracle 12c在视图上比较last_ddl_time

时间:2016-10-31 18:50:08

标签: sql oracle

我有一个两个模式的视图列表,除了last_ddl_time之外,其中一切都是相同的。

select owner, object_name, last_ddl_time
from all_objects
where object_type = upper('view')
and owner in ('SC1', 'SC2')
order by 2,1,3;

生成正确的数据,但我希望SQL只比较last_ddl_time并且只有当SC1的last_ddl_time大于SC2的last_ddl_time时才返回数据。

我尝试创建一个进行比较的子查询,但无法获得用于生成比较数据的语法。

该对象是在SC1中更新同名视图之前查找在SC2中更新的视图。

任何和所有建议&洞见表示赞赏。

3 个答案:

答案 0 :(得分:4)

您可以使用内联视图(或类似的CTE)查找每个架构的DDL时间,例如使用aggregate和windowing子句,并通过比较外部查询中的这些值来过滤结果:

select owner, object_name, last_ddl_time
from (
  select owner, object_name, last_ddl_time,
    max(case when owner = 'SC1' then last_ddl_time end)
      over (partition by object_name) as sc1_time,
    max(case when owner = 'SC2' then last_ddl_time end)
      over (partition by object_name) as sc2_time
  from all_objects
  where object_type = 'VIEW'
  and owner in ('SC1','SC2')
)
where sc1_time > sc2_time
order by 2,1,3;

您也可以使用它来并排显示两个DDL时间:

select object_name, sc1_time, sc2_time
from (
  select owner, object_name,
    max(case when owner = 'SC1' then last_ddl_time end)
      over (partition by object_name) as sc1_time,
    max(case when owner = 'SC2' then last_ddl_time end)
      over (partition by object_name) as sc2_time
  from all_objects
  where object_type = 'VIEW'
  and owner in ('SC1','SC2')
)
where sc1_time > sc2_time
and owner = 'SC1'
order by 1;

答案 1 :(得分:2)

如果您确定两个架构上都有相同的视图,我会选择简单的内部连接

QUAL_PX_PlanConnexion@PYHT_YH01_G1_HM_9002-01_SFINP.rpt.f07_20161027115330

答案 2 :(得分:2)

在Oracle 12中,您可以使用match_recognize。这个例子来自我的Oracle副本;我将SCOTT模式复制到我自己的模式(称为INTRO),因此我们有一些要测试的对象。

select object_type, object_name, scott_last_ddl_time, intro_last_ddl_time
from   all_objects
match_recognize (
  partition by object_type, object_name
  order     by last_ddl_time
  measures     s.last_ddl_time as scott_last_ddl_time,
               i.last_ddl_time as intro_last_ddl_time
  pattern      (s i)
  define       s as s.owner = 'SCOTT',
               i as i.owner = 'INTRO' and i.last_ddl_time > s.last_ddl_time
)
order by object_type, object_name, scott_last_ddl_time  -- ORDER BY is optional
;

OBJECT_TYPE OBJECT_NAME SCOTT_LAST_DDL_TIME INTRO_LAST_DDL_TIME
----------- ----------- ------------------- -------------------
INDEX       PK_DEPT     2016-10-05 18:18:34 2016-10-05 21:52:31
INDEX       PK_EMP      2016-10-05 18:18:34 2016-10-05 21:52:31
TABLE       BONUS       2016-10-05 18:18:35 2016-10-05 21:52:31
TABLE       DEPT        2016-10-05 18:18:34 2016-10-05 21:52:33
TABLE       EMP         2016-10-05 18:18:34 2016-10-05 21:52:34
TABLE       SALGRADE    2016-10-05 18:18:35 2016-10-05 21:52:31

 6 rows selecte

如果要将last_ddl_time与非严格(>=而非>)进行比较,那么您甚至不需要在{{1}的定义中进行比较,i中的order by子句会对比较进行比较。