如何使用完全外部联接来联接多个select语句。错误:子查询返回多个行

时间:2019-02-13 20:16:50

标签: sql oracle

自昨天以来,我一直在与这段代码进行斗争,当我对该代码的每个部分进行单元测试时,它都在工作,但是我需要将它们放在一起以生成一个输出结果。这是下面的完整代码:但是给子查询返回的行多。

SELECT NVL(TO_CHAR(D_TRANS.TRANS), 'NULL') AS ID, 'HEADER', D_SPILL.status, 
      (SELECT L_APPLICATION.APPLICATION 
         FROM L_APPLICATION   L_APPLICATION 
        WHERE LANGUAGE = 2 AND APPLICATION = D_TRANS.APPLICATION) 
      AS CASE_TYPE, 
      NVL(TO_CHAR(D_TRANS.UNIT_IN_CHARGE), 'NULL') AS UNIT_IN_CHARGE,
      NVL(TO_CHAR(D_TRANS.PERSON_IN_CHARGE), 'NULL') AS PERSON_IN_CHARGE,
      NVL(TO_CHAR(D_TRANS.STATUS), 'NULL') AS CASE_STATUS, 
      NVL(TO_CHAR(D_TRANS.DEADLINE), 'NULL') AS INTERNAL_DEADLINE,
            ( select   xmlquery('distinct-values(//text())' passing xmldoc returning content).getclobVal()
       FROM  ( select  d_synergi_category.trans, 
       coalesce(max(case when language =  2 then description end), 'NULL'), 
         XMLELEMENT(root,xmlagg(XMLELEMENT(e,description,','))
                  ) xmldoc
           from L_CASE_CATEGORY  
         LEFT JOIN  d_synergi_category ON  d_synergi_category.case_category          = L_CASE_CATEGORY.case_category 
         group by  d_synergi_category.trans

             )
                      )              
                  FROM D_TRANS
             FULL OUTER  JOIN D_SPILL
              ON D_TRANS.TRANS=D_SPILL.TRANS
               ORDER BY D_TRANS.TRANS DESC;

               If I remove the part code  below with xmltagg and test both parts of the code separately it is working. 

第一部分单独工作

  ( select   xmlquery('distinct-values(//text())' passing xmldoc returning content).getclobVal()
       FROM  ( select  d_synergi_category.trans, 
       coalesce(max(case when language =  2 then description end), 'NULL'), 
         XMLELEMENT(root,xmlagg(XMLELEMENT(e,description,','))
                  ) xmldoc
  from L_CASE_CATEGORY  
     LEFT JOIN  d_synergi_category ON  d_synergi_category.case_category =         L_CASE_CATEGORY.case_category 
  group by  d_synergi_category.trans

) )

第二部分是:

SELECT NVL(TO_CHAR(D_TRANS.TRANS), 'NULL') AS ID, 'HEADER',D_SPILL.status, 
   (SELECT L_APPLICATION.APPLICATION FROM L_APPLICATION 
    WHERE L_APPLICATION WHERE LANGUAGE = 2 
     AND APPLICATION = D_TRANS.APPLICATION) AS CASE_TYPE , 
    NVL(TO_CHAR(D_TRANS.UNIT_IN_CHARGE), 'NULL') AS UNIT_IN_CHARGE, 
    NVL(TO_CHAR(D_TRANS.PERSON_IN_CHARGE), 'NULL') AS PERSON_IN_CHARGE, 
    NVL(TO_CHAR(D_TRANS.STATUS), 'NULL') AS CASE_STATUS , 
    NVL(TO_CHAR(D_TRANS.DEADLINE), 'NULL') AS INTERNAL_DEADLINE
  FROM D_TRANS
  FULL OUTER  JOIN D_SPILL
       ON D_TRANS.TRANS=D_SPILL.TRANS
   ORDER BY D_TRANS.TRANS DESC;

3 个答案:

答案 0 :(得分:1)

查询(SELECT rtrim(xmlagg( ...必须恰好返回一行。

我们没有您的表或数据,但似乎您没有将其表(L_CASE_CATEGORYD_SYNERGI_CATEGORY)与主查询的FROM子句({{ 1}},TRANS)。我建议您这样做,然后看看会发生什么。

[编辑]

这就是我的意思:

D_SPILL

答案 1 :(得分:0)

@Littlefoot说了什么,但我将猜测如何将表连接起来,以便为您提供示例。

SELECT NVL(TO_CHAR(D_TRANS.TRANS), 'NULL') AS ID, 'HEADER',D_SPILL.status, 
   (SELECT L_APPLICATION.APPLICATION FROM L_APPLICATION 
    WHERE L_APPLICATION WHERE LANGUAGE = 2 
     AND APPLICATION = D_TRANS.APPLICATION) AS CASE_TYPE , 
    NVL(TO_CHAR(D_TRANS.UNIT_IN_CHARGE), 'NULL') AS UNIT_IN_CHARGE, 
    NVL(TO_CHAR(D_TRANS.PERSON_IN_CHARGE), 'NULL') AS PERSON_IN_CHARGE, 
    NVL(TO_CHAR(D_TRANS.STATUS), 'NULL') AS CASE_STATUS , 
    NVL(TO_CHAR(D_TRANS.DEADLINE), 'NULL') AS INTERNAL_DEADLINE,
    (SELECT  rtrim(xmlagg( 
             XMLELEMENT(e,L_CASE_CATEGORY.DESCRIPTION,',').EXTRACT('//text()')
              ).GetClobVal(),',') 
     FROM L_CASE_CATEGORY
     INNER JOIN   D_SYNERGI_CATEGORY  on  -- changed
           D_SYNERGI_CATEGORY.CASE_CATEGORY = L_CASE_CATEGORY.CASE_CATEGORY   
         AND L_CASE_CATEGORY.LANGUAGE = 2 
     WHERE d_synergi_category.trans = D_TRANS.TRANS -- added this line
     GROUP BY  D_SYNERGI_CATEGORY.CASE_CATEGORY, d_synergi_category.trans) AS CAT_DESC_LIST
  FROM D_TRANS
  FULL OUTER  JOIN D_SPILL
       ON D_TRANS.TRANS=D_SPILL.TRANS
   ORDER BY D_TRANS.TRANS DESC;

编辑:根据您的评论进行更新。如果提供表结构和示例数据,这会容易得多。

答案 2 :(得分:0)

您的子查询SELECT rtrim(xmlagg(...CASE_CATEGORYtrans返回一行,原因如下:

GROUP BY D_SYNERGI_CATEGORY.CASE_CATEGORY, d_synergi_category.trans

但是,当将其放在主查询的select子句中时,您不希望它返回每个主查询行的所有那些行,而是与主查询行匹配的那一行(就像您找到匹配的应用程序行一样) WHERE APPLICATION = D_TRANS.APPLICATION)。

因此,删除GROUP BY子句,并用WHERE子句代替。像

(
  SELECT
    RTRIM(XMLAGG( 
       XMLELEMENT(e, cc.description,',').EXTRACT('//text()')
    ).GetClobVal(),',') 
  FROM l_case_category cc
  JOIN d_synergi_category sc ON sc.case_category = cc.case_category   
  WHERE sc.case_category = d_spill.case_category -- <=== here
    AND sc.trans = d_trans.trans -- <=== and here
    and cc.language = 2
)

(只需用您的真实条件替换我的条件。只有您知道表之间的关系。我什至很难弄清楚查询应该返回什么。我不明白为什么您需要完全外部联接在一个单独的查询中使用D_SPILL,而没有在select子句中使用其任何列。我也不知道为什么在另一个查询中在D_SYNERGI_CATEGORY上外部联接L_CASE_CATEGORY.LANGUAGE = 2。这看起来很奇怪。)