几个连接子查询是测试多个表的唯一方法吗?

时间:2012-10-10 20:15:29

标签: sql oracle

我正在测试几个表中的最新日期在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 个答案:

答案 0 :(得分:2)

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

当结果是&gt; 1,然后其中一个表有不同的last_month。

答案 1 :(得分: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