2012-10-10 38 views
0

我測試的幾個表中的最新日期都在oracle中相互匹配。我想出的SQL看起來像:是否有多個連接的子查詢用於測試多個表?

select ICEAG.process_month 
from (
    select * 
    from (
     select process_month 
     from TABLE1 
     group by process_month 
     order by process_month desc 
    ) 
    where rownum <=1 
) ICEAG 
join (
    select * 
    from (
     select process_month 
     from TABLETWO 
     group by process_month 
     order by process_month desc 
    ) 
    where rownum <=1 
) GAI on (ICEAG.process_month = GAI.process_month) 

這個工程,但我需要檢查大約12個表。我應該繼續加入更多的子查詢,還是有更好的方法?

回答

2
SELECT COUNT(*) FROM (
(SELECT MAX(process_month) FROM TABLE1) 
    UNION 
(SELECT MAX(process_month) FROM TABLE2) 
    UNION 
(SELECT MAX(process_month) FROM TABLE3) 
); 

當結果大於1時,其中一個表具有不同的最後一個process_month。

1

如果您只是想匹配輸出,您可以使用相交。

select process_month 
    from TABLETWO 
    group by process_month 
    order by process_month desc 
    where rownum <=1 
INTERSECT 
    select process_month 
    from TABLE1 
    group by process_month 
    order by process_month desc 
    where rownum <=1