2016-10-31 102 views
1

我有兩個模式中的視圖列表,其中除了last_ddl_time之外的所有內容都相等。Oracle 12c在視圖中比較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比last_ddl_time爲SC2大。

我已經嘗試創建一個子查詢,使比較,但不能得到的語法產生的比較數據。

目標是在SC1中更新相同名稱的視圖之前查找在SC2中更新的視圖。

任何和所有建議&深表讚賞。

+0

請提供樣品數據和所需的結果。 –

回答

4

您可以使用內聯視圖(或類似的CTE)來查找每個模式的DDL時間,例如,使用聚合和窗口子句和過濾器的結果通過在外部查詢比較這些值:

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; 
+0

完美!謝謝Alex。像一個魅力.. – user761758

2

如果你肯定有兩個模式我會用簡單的內部走同樣的觀點加入

with sc1 as (
    select owner, object_name, last_ddl_time 
     from all_objects 
     where object_type = 'VIEW' and owner = 'SC1'), 
    sc2 as (
    select owner, object_name, last_ddl_time 
     from all_objects 
     where object_type = 'VIEW' and owner = 'SC2') 
    select sc1.object_name, sc1.last_ddl_time from sc1 inner join sc2 
     on (sc1.object_name = sc.object_name) 
    where sc1.last_ddl_time > sc2.last_ddl_time; 
+0

逗號後面last_ddl_time需要消失,但它工作正常..謝謝! – user761758

+0

@ user761758逗號已移除。 – Kacper

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是要比較非嚴格(>=而不是>),那麼你甚至不需要在i定義的比較,比較會由order by條款被照顧match_recognize