在oracle 11.2 select子句中使用join的相关子查询不起作用

时间:2018-06-07 22:06:05

标签: oracle oracle11g correlated-subquery

以下相关子查询在12c中运行良好,但在11.2中提到错误时失败。有人能帮忙吗?提前谢谢。

使用11.2.0.4

SQL> column member1 format A35
column member2 format A35
select a.group#, a.SEQUENCE#, a.ARCHIVED, a.STATUS,
(SELECT Member from (SELECT Member, ROWNUM AS RN FROM v$logfile b where b.group#=a.group#) WHERE RN = 1) as Member1,
(SELECT Member from (SELECT Member, ROWNUM AS RN FROM v$logfile b where b.group#=a.group#) WHERE RN = 2) as Member2,
(a.BYTES/1024/1024)
from v$log a;SQL> SQL>   2    3    4    5  
(SELECT Member from (SELECT Member, ROWNUM AS RN FROM v$logfile b where b.group#=a.group#) WHERE RN = 1) as Member1,
                                                                                 *
ERROR at line 2:
ORA-00904: "A"."GROUP#": invalid identifier

使用12c

GROUP# SEQUENCE#  ARC STATUS   MEMBER1                        MEMBER2                        (A.BYTES/1024/1024)
------ ---------- --- -------- ------------------------------ ------------------------------ -------------------
 7     4901       YES INACTIVE /u02/oradata/PROD/redo06.log   /u02/oradata/PROD/redo07.log                   810
 9     4902       YES INACTIVE /u02/oradata/PROD/redo9.log    /u03/oradata/PROD/redo9.log                    808
10     4903       NO  CURRENT  /u02/oradata/PROD/redo10.log   /u03/oradata/PROD/redo10.log                   808

1 个答案:

答案 0 :(得分:0)

如果我错了,请有人纠正我,但如果没有ORDER BY条款,你不能保证得到不同的成员。

我相信这个SQL会为你带来理想的结果。我已在Oracle 11上测试过它。

WITH
    aset
    AS
        (SELECT MEMBER
              , group#
              , ROW_NUMBER ()
                    OVER (
                        PARTITION BY group# ORDER BY MEMBER
                    )
                    rn
           FROM v$logfile)
SELECT a.group#
     , a.sequence#
     , a.archived
     , a.status
     , member1
     , member2
  FROM v$log a
       LEFT OUTER JOIN (SELECT MEMBER AS member1, group#
                          FROM aset
                         WHERE rn = 1) aset1
           ON a.group# = aset1.group#
       LEFT OUTER JOIN (SELECT MEMBER AS member2, group#
                          FROM aset
                         WHERE rn = 2) aset2
           ON a.group# = aset2.group#;