【问题标题】:Oracle SQL: rtrim and group byOracle SQL:rtrim 和 group by
【发布时间】:2015-05-09 03:33:52
【问题描述】:

我正在执行以下查询并收到“不是单组组功能”错误。

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()'), ',') 

但是当我尝试这样做时,我收到了一个错误:

ORA-00937 not a single-group group function

我没有在 select 语句中使用任何聚合。我应该怎么做才能写 rtrim(... ) 没有错误?

提前致谢。

【问题讨论】:

    标签: sql oracle group-by string-aggregation


    【解决方案1】:

    您需要删除您的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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 2014-11-08
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      相关资源
      最近更新 更多