ORA-00923: 在 Oracle 中使用 over (partition by) 与 Xmlagg
ORA-00923: Use over (partition by) with Xmlagg in Oracle
我有一个下面表格的查询,我需要它按特定列进行分区,但是当我这样放置它时,它给我错误:在预期的地方找不到来自关键字
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')
您似乎想要 select table 名称及其包含的列。
LISTAGG
,在这种情况下,不需要 OVER
子句,因为您必须使用 GROUP BY
(如果您想获取 table 名称作为好吧)所以它将完成分区工作;此外,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>
我有一个下面表格的查询,我需要它按特定列进行分区,但是当我这样放置它时,它给我错误:在预期的地方找不到来自关键字
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')
您似乎想要 select table 名称及其包含的列。
LISTAGG
,在这种情况下,不需要 OVER
子句,因为您必须使用 GROUP BY
(如果您想获取 table 名称作为好吧)所以它将完成分区工作;此外,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>