1

我有一个下面表格的查询,我需要它按某个列进行分区,但是当我这样放置它时,它给了我错误:from keyword not found where expected

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 一起正常运行,就像下面的 list 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')
4

1 回答 1

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>
于 2019-04-27T07:27:48.593 回答