简体   繁体   中英

Oracle SQL: rtrim and group by

I was doing like the following query and received "not a single-group group function" error.

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);

I did left join between table1 and table2 and I need to aggregate multiple rows into one row. So I wrote:

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

But when I attempt to do this, I received an error:

ORA-00937 not a single-group group function

I am not using any aggregations in select statement. What should I do to write rtrim(... ) without the error?

Thanks in advance.

You need to remove your DISTINCT and use GROUPING instead, as you use an aggregate function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM