ORA-00923:在Oracle中与Xmlagg一起使用(分区)

时间:2019-04-26 21:02:06

标签: xml oracle aggregation partition-by

我对下面的表格有一个查询,我需要它按某个列进行分区,但是当我这样放置它时,它给我的错误是:from关键字不在期望的位置

Select distinct t_name,  rtrim(xmlagg(xml element(e, text, ',').extract('//text()') order by c_id).getclobval(), ',' ) over (partition by t_name) col_list from all_cls where schema ='a' and table in ('tableA' , 'tableB')

问题是什么,该如何解决,以便它与xmlagg一起正常工作,就像下面对带有列表agg的查询一样:

Select distinct t_name,  listagg(text ',' ) within group(order by c_id) over (partition by t_name) col_list from all_cls where schema ='a' and table in ('tableA' , 'tableB')

1 个答案:

答案 0 :(得分:0)

似乎您要选择表名和它包含的列。

在这种情况下,

LISTAGG不需要OVER子句,因为您必须使用GROUP BY(如果您还想获取表名),因此它将做分区工作;同样,GROUP BY-使得DISTINCT变得不必要。

类似这样的东西:

SQL> select table_name,
  2    listagg(column_name, ',' ) within group(order by column_id) col_list
  3  from all_tab_cols
  4  where owner = 'SCOTT'
  5    and table_name in ('DEPT' , 'BONUS')
  6  group by table_name;

TABLE_NAME COL_LIST
---------- --------------------------------------------------
BONUS      ENAME,JOB,SAL,COMM
DEPT       DEPTNO,DNAME,LOC

SQL>

XMLAGG版本如下:就像上面一样,GROUP BY不需要特殊分区:

SQL> select table_name,
  2    rtrim(xmlagg(xmlelement(e, column_name || ',').extract('//text()')
  3      order by column_id).getclobval(), ',' ) col_list
  4  from all_tab_cols
  5  where owner = 'SCOTT'
  6    and table_name in ('DEPT' , 'BONUS')
  7  group by table_name;

TABLE_NAME COL_LIST
---------- --------------------------------------------------
BONUS      ENAME,JOB,SAL,COMM
DEPT       DEPTNO,DNAME,LOC

SQL>