与 join in oracle 11.2 select 子句相关的子查询不起作用
Correlated subquery with join in oracle 11.2 select clause doesn't works
以下相关子查询在 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
如果我错了,请大家指正,但是如果你的子选择中没有 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#;
以下相关子查询在 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
如果我错了,请大家指正,但是如果你的子选择中没有 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#;