Oracle SQL - 子查询

时间:2012-05-31 10:34:53

标签: sql oracle oracle11g

我在EM做报告,我需要弄清楚这里的事情

我有这个查询:

SELECT
*
FROM
(
    SELECT DISTINCT
    patch.host as "PHost",
    patch.home_location as "PDirectory",
    patch.home_name as "PHome",
    MAX(patch.INSTALLATION_TIME) as "Patched (Date)",
    MAX(patch.PATCH_RELEASE) as "PVersion",
    listagg(patch,',') WITHIN GROUP (ORDER BY patch) "Patches"
    FROM
    mgmt$applied_patches patch
    GROUP BY patch.host, patch.home_location,patch.home_name
    ORDER BY patch.host, patch.home_location
) "PCH",
(
    SELECT DISTINCT 
    T1.PROPERTY_VALUE as "MHost",
    T2.PROPERTY_VALUE as "MDirectory",
    T3.PROPERTY_VALUE as "MVersion",
    count(T4.PROPERTY_VALUE) as "Count of SID",
    listagg(T4.PROPERTY_VALUE,',') WITHIN GROUP (ORDER BY T4.PROPERTY_VALUE) as "SID"
    FROM
    MGMT$TARGET_PROPERTIES T1,
    MGMT$TARGET_PROPERTIES T2,
    MGMT$TARGET_PROPERTIES T3,
    MGMT$TARGET_PROPERTIES T4
    WHERE
    T1.TARGET_GUID = T2.TARGET_GUID
    and T1.TARGET_GUID = T3.TARGET_GUID
    and T1.TARGET_GUID = T4.TARGET_GUID
    and T1.PROPERTY_NAME = 'MachineName'
    and T2.PROPERTY_NAME = 'OracleHome'
    and T3.PROPERTY_NAME = 'Version'
    and T4.PROPERTY_NAME = 'SID'
    GROUP BY T1.PROPERTY_VALUE, T2.PROPERTY_VALUE, T3.PROPERTY_VALUE
) "MGM"
WHERE
PDirectory = MDirectory

我收到错误ORA-00904:“MDIRECTORY”:....

我尝试了很多组合! (PCH.PDirectory = MGM.MDirectory,......)没有任何作用

欢呼声

2 个答案:

答案 0 :(得分:2)

Oracle中的混合案例名称令人憎恶,你刚被他们咬了。

使用:

"PDirectory" = "MDirectory"

..或更好仍然不使用需要引用的特殊名称。

答案 1 :(得分:1)

嗯......

这不起作用:

SELECT * FROM
(SELECT DUMMY AS  "PDirectory" FROM DUAL) "PCH",
(SELECT DUMMY AS  "MDirectory" FROM DUAL) "MGR"
WHERE PDIRECTORY = MDIRECTORY

但这有效:

SELECT * FROM
(SELECT DUMMY AS  "PDirectory" FROM DUAL) "PCH",
(SELECT DUMMY AS  "MDirectory" FROM DUAL) "MGR"
where "PCH"."PDirectory" = "MGR"."MDirectory"

相应地更改您的查询。