子查询返回多行

时间:2019-02-12 13:25:58

标签: oracle

我有2个表正试图从中提取信息。这是下表的示例,因为它们是大表。 我希望表1上的描述位于表2的KEY_EVENT列中,并且唯一的公共列是APP列。 但是错误已经到来,子查询不能返回多行。

<trans-unit>

此外,我想使用-如果我们具有多个描述的通用应用程序,那么我想用LISTAGG来连接描述

TABLE 1

 APP    LANGUAGE    DESCRIPTION 
195          12         Involved person 
195          27       Involved person   
 196           1         Involvert legemiddel   
196           2    Involved drug    
196           3    Involverad lakemedel 
196          4     Involveret l?gemiddel    
196          12    Involved drug    
196          27    Involved drug    
197          1     Eksponeringsverdier  
197          2     Exposure values  
197          3     Exponeringsvarden    
197          4     Eksponeringsv?rdier  
197         12     Exposure values  
197         27     Exposure values  
198          1     Indikatorer  
198          2     Indicators   
198          3     Indikatorer  
198          4     Indikatorer  
198         12     Indicators   
198         27     Indicators   
199         1      Generell klassifisering  
199         2      General classification   
199         3      Generell klassificering  
199         4      Generel klassifisering   
199        12      General classification   
199         27     General classification   


         TABLE 2
 TRANS    APP
 1         195
 2         195
 3         196
 4         196
 5         196
 6         196
 7         196
 8         196
 9         197
 10        197
11         197
12         197
13         197
14         197
15         198
16         198
17         198
18         198
19         198
20         198
21         199
22         199
23         199
24         199
25         199
26         199

3 个答案:

答案 0 :(得分:1)

您需要使用聚合来合并行。您无法在一个单元格中显示所有行。 因此,LISTAGG是您想要的浓缩字符串。

这是一个示例查询:

SELECT  
NVL(TO_CHAR(TRANS.ID), 'NULL') AS ID, 
'HEADER',
NVL(
    TO_CHAR(
            (
                SELECT LISTAGG ( L_APP.DESCRIPTION ,',' ) 
                FROM  L_APP 
                WHERE  EXISTS  ( 
                                SELECT 1 
                                FROM TRANS 
                                WHERE L_APP.APP =                  TRANS.APP AND LANGUAGE = 2
                            )

           )
        ), 'NULL'
) AS KEY_EVENT

答案 1 :(得分:1)

据我所知,您需要用英语(language = 2)作为KEY_EVENT列的描述,以及所有其他语言的现有描述的列表。请查看此语法。我还附上了一个演示,演示了该查询如何处理您提供的数据。

dbfiddle demo

select trans, t2.app, 
       nvl(max(case when language = 2 then description end), 'NULL') key_event,
       listagg(description, ', ') within group (order by t1.language) list
  from table2 t2 
  left join table1 t1 on t1.app = t2.app 
  group by trans, t2.app
  order by trans;

1227之类的内容也是英文描述,如果您想使用它们(如果没有2的话),请运行以下命令:

select trans, t2.app,  
       coalesce(max(case when language =  2 then description end), 
                max(case when language = 12 then description end), 
                max(case when language = 27 then description end), 
               'NULL') key_event,
       listagg(description, ', ') within group (order by t1.language) list
  from table2 t2 
  left join table1 t1 on t1.app = t2.app 
  group by trans, t2.app
  order by trans;

答案 2 :(得分:0)

我已经可以在此页面中使用此答案,并且在并置为一个文件的过程中未创建任何重复文件:

subquery eror and too many values using xmllagg

最终代码是

  select app, key_event,  xmlquery('distinct-values(//text())' passing xmldoc returning content).getclobVal()
from (
select  t2.app,  
       coalesce(max(case when language =  2 then description end), 
                max(case when language = 12 then description end), 
                max(case when language = 27 then description end), 
               'NULL') key_event,
         XMLELEMENT(root,xmlagg(XMLELEMENT(e,description,','))
                  ) xmldoc
  from table2 t2 
  left join table1 t1 on t1.app = t2.app 
  group by trans, t2.app
   order by trans);