2017-05-19 66 views
0

我不明白爲什麼這個查詢不起作用。oracle不是一個具有嵌套查詢功能的組

在3臺服務器上工作的原始查詢。

SELECT (
     SELECT to_char(RTRIM(XMLAGG(XMLELEMENT(e, TEXT, ',').EXTRACT('//text()')).GetClobVal(), ',')) CM_LINES 
     FROM (
      SELECT DISTINCT to_char(cmline) TEXT 
      FROM CCP.comms_matrix_data 
      WHERE comms_matrix_id = :pkid 
       AND src_net = t1.SRC_NET 
       AND dst_net = t1.DST_NET 
      ORDER BY cmline 
      ) 
     ) CM_LINES 
    ,t1.SRC_NET 
    ,t1.DST_NET 
    ,t1.SRC_NETZONE 
    ,t1.DST_NETZONE 
    ,t1.RPA 
    ,t1.RPN 
FROM CCP.comms_matrix_data t1 
WHERE (t1.COMMS_MATRIX_ID = :pkid) 
    AND (LOWER(t1.STATUS) LIKE '%implement%') 
GROUP BY t1.SRC_NET 
    ,t1.DST_NET 
    ,t1.SRC_NETZONE 
    ,t1.DST_NETZONE 
    ,t1.RPA 
    ,t1.RPN 

在開發服務器這樣的錯誤與ORA-00904: "T1"."DST_NET": invalid identifier 00904. 00000 - "%s: invalid identifier"

我的開發服務器上它不這樣工作,我決定重寫查詢:開發服務器上

SELECT CM_LINES.CM_LINES 
    ,t1.SRC_NET 
    ,t1.DST_NET 
    ,t1.SRC_NETZONE 
    ,t1.DST_NETZONE 
    ,t1.RPA 
    ,t1.RPN 
FROM CCP.comms_matrix_data t1, 
(
     SELECT to_char(RTRIM(XMLAGG(XMLELEMENT(e, TEXT, ',').EXTRACT('//text()')).GetClobVal(), ',')) CM_LINES, src_net, dst_net 
     FROM (
      SELECT DISTINCT to_char(cmline) TEXT, src_net, dst_net 
      FROM CCP.comms_matrix_data 
      WHERE comms_matrix_id = :pkid 

      ORDER BY cmline 
      ) 
     ) CM_LINES 
WHERE (t1.COMMS_MATRIX_ID = :pkid) 
    AND (LOWER(t1.STATUS) LIKE '%implement%') 
    AND CM_LINES.src_net = t1.SRC_NET 
    AND CM_LINES.dst_net = t1.DST_NET 
GROUP BY CM_LINES.CM_LINES 
    ,t1.SRC_NET 
    ,t1.DST_NET 
    ,t1.SRC_NETZONE 
    ,t1.DST_NETZONE 
    ,t1.RPA 
    ,t1.RPN; 

這個錯誤與ORA-00937: not a single-group group function 00937. 00000 - "not a single-group group function"

@Gordon

UPDATE

SELECT x.CM_LINES 
    ,t1.SRC_NET 
    ,t1.DST_NET 
    ,t1.SRC_NETZONE 
    ,t1.DST_NETZONE 
    ,t1.RPA 
    ,t1.RPN 
FROM CCP.comms_matrix_data t1 
JOIN (
    SELECT to_char(RTRIM(XMLAGG(XMLELEMENT(e, cml.TEXT, ',').EXTRACT('//text()')).GetClobVal(), ',')) CM_LINES 
     ,src_net 
     ,dst_net 
    FROM (
     SELECT DISTINCT to_char(cmline) TEXT 
      ,src_net 
      ,dst_net 
     FROM CCP.comms_matrix_data 
     WHERE comms_matrix_id = :pkid 
     ORDER BY cmline 
     ) cml ON cml.src_net = t1.SRC_NET /* sql developer reports problem here*/ 
     AND cml.dst_net = t1.DST_NET 
    WHERE t1.COMMS_MATRIX_ID = :pkid 
     AND LOWER(t1.STATUS) LIKE '%implement%' 
    GROUP BY src_net 
     ,dst_net 
    ) x 
GROUP BY x.CM_LINES 
    ,t1.SRC_NET 
    ,t1.DST_NET 
    ,t1.SRC_NETZONE 
    ,t1.DST_NETZONE 
    ,t1.RPA 
    ,t1.RPN; 

更新2

如果我嘗試下面我通過ORA-00905: missing keyword 00905. 00000 - "missing keyword"

SELECT x.CM_LINES 
    ,t1.SRC_NET 
    ,t1.DST_NET 
    ,t1.SRC_NETZONE 
    ,t1.DST_NETZONE 
    ,t1.RPA 
    ,t1.RPN 
FROM CCP.comms_matrix_data t1 
JOIN (
    SELECT to_char(RTRIM(XMLAGG(XMLELEMENT(e, cml.TEXT, ',').EXTRACT('//text()')).GetClobVal(), ',')) CM_LINES 
     ,src_net 
     ,dst_net 
    FROM (
     SELECT DISTINCT to_char(cmline) TEXT 
      ,src_net 
      ,dst_net 
     FROM CCP.comms_matrix_data 
     WHERE comms_matrix_id = :pkid 
     ORDER BY cmline 
     ) cml 
    join cml ON cml.src_net = t1.SRC_NET 
     AND cml.dst_net = t1.DST_NET 
    WHERE t1.COMMS_MATRIX_ID = :pkid 
     AND LOWER(t1.STATUS) LIKE '%implement%' 
    GROUP BY src_net 
     ,dst_net 
    ) x /* should there not be an on clause here? */ 
GROUP BY x.CM_LINES /* error here */ 
    ,t1.SRC_NET 
    ,t1.DST_NET 
    ,t1.SRC_NETZONE 
    ,t1.DST_NETZONE 
    ,t1.RPA 
    ,t1.RPN; 
+1

你想解決哪個問題?你原來的問題是因爲對'T1.DST_NET'的引用嵌套得太深了。 [查看此SO發佈瞭解更多詳細信息](http://stackoverflow.com/q/1233910/146325) – APC

+0

您有一個查詢在三臺服務器上運行得非常好,但在Dev上無法運行。爲什麼只爲Dev重寫查詢?我不會花費所有的努力,而只是試着弄清楚爲什麼你會得到無效的標識符異常。我的猜測是,在開發表中存在不同的架構 –

+0

@ChristianPalmer該表是在相同的架構 – shorif2000

回答

1

在組得到錯誤您在子查詢需要一個GROUP BY

SELECT CM_LINES, t1.SRC_NET, t1.DST_NET, 
     t1.SRC_NETZONE, t1.DST_NETZONE, t1.RPA, t1.RPN 
FROM CCP.comms_matrix_data t1 JOIN 
    (SELECT to_char(RTRIM(XMLAGG(XMLELEMENT(e, TEXT, ',').EXTRACT('//text()')).GetClobVal(), ',')) as CM_LINES, 
      src_net, dst_net 
     FROM (SELECT to_char(cmline) as TEXT, src_net, dst_net, 
        ROW_NUMBER() OVER (PARTITION BY src_net, dst_net ORDER BY cmline DESC) as seqnum 
      FROM CCP.comms_matrix_data 
      WHERE comms_matrix_id = :pkid 
      ) CM_LINES 
      ON CM_LINES.src_net = t1.SRC_NET AND 
       CM_LINES.dst_net = t1.DST_NET AND 
       seqnum = 1 
     WHERE t1.COMMS_MATRIX_ID = :pkid AND 
      LOWER(t1.STATUS) LIKE '%implement%' 
     GROUP BY src_net, dst_net 
    ) x 
GROUP BY CM_LINES, t1.SRC_NET, t1.DST_NET 
     t1.SRC_NETZONE, t1.DST_NETZONE, t1.RPA, t1.RPN; 

XMLAGG()是一個聚合函數。

+0

我收到一個錯誤'ORA-00907:缺少右括號 00907. 00000 - 「缺少右括號」這是在CM_LINES ON CM_LINES.src_net = t1.SRC_NET' – shorif2000

+0

@ shorif2000。 。 。別名中有一個錯誤,但我沒有看到不平衡的括號。 –

+0

我用正確的別名更新了你的sql更新。還是一樣的錯誤 – shorif2000