2015-05-09 51 views
0

我正在做如下查询并收到“没有单组功能”错误。Oracle SQL:rtrim和group by

select distinct a.col1, 
     a.col2, 
     rtrim(xmlagg(xmlelement(e, b.col1 ||',')).extract('//text()'), ','), 
     rtrim(xmlagg(xmlelement(e, b.col2 ||',')).extract('//text()'), ',') 
from table1 a 
where a.col3 like '%string%' 
left join table2 b on (a.pid = b.pid); 

我做了table1和table2之间的左连接,我需要将多行聚合成一行。所以我写了:

rtrim(xmlagg(xmlelement(e, b.col1 ||',')).extract('//text()'), ','), 
rtrim(xmlagg(xmlelement(e, b.col2 ||',')).extract('//text()'), ',') 

但是,当我试图这样做,我收到了一个错误:我不是在SELECT语句中使用任何聚合

ORA-00937 not a single-group group function 

。我应该怎样写rtrim(...)而不出错?

在此先感谢。

回答

0

您需要删除DISTINCT并使用GROUPING代替,因为您使用聚合函数。

select a.col1, 
     a.col2, 
     rtrim(xmlagg(xmlelement(e, b.col1 ||',')).extract('//text()'), ','), 
     rtrim(xmlagg(xmlelement(e, b.col2 ||',')).extract('//text()'), ',') 
from table1 a 
where a.col3 like '%string%' 
left join table2 b on (a.pid = b.pid) 
GROUP BY a.col1, 
     a.col2