Sql 如何从listagg中删除重复项
这是我的代码:Sql 如何从listagg中删除重复项,sql,oracle,Sql,Oracle,这是我的代码: SELECT rule, DATASET, type, LISTAGG(source,';') within GROUP (ORDER BY source) AS source, TARGET, LISTAGG(filter,';') within GROUP (ORDER BY filter) AS filter FROM demo_table; 我的问题是我使用的两个listag()。在两个源和一个过
SELECT rule,
DATASET,
type,
LISTAGG(source,';') within GROUP (ORDER BY source) AS source,
TARGET,
LISTAGG(filter,';') within GROUP (ORDER BY filter) AS filter
FROM demo_table;
我的问题是我使用的两个
listag()
。在两个源和一个过滤器的情况下,它给出了重复的过滤器,反之亦然。我可以添加哪些内容来获得源和筛选器的确切数量?使用Distinct INDER LISTAGG()
在LISTAGG()内部使用Distinct
首先选择所需的不同值,然后对其应用LISTAGG。下面是一个基于Scott模式的示例
SQL> -- Duplicate jobs within the department
SQL> select deptno, listagg(job, ', ') within group (order by job) jobs
2 from emp
3 group by deptno;
DEPTNO JOBS
---------- ------------------------------------------------------------
10 CLERK, MANAGER, PRESIDENT
20 ANALYST, ANALYST, CLERK, CLERK, MANAGER
30 CLERK, MANAGER, SALESMAN, SALESMAN, SALESMAN, SALESMAN
SQL>
SQL> -- This won't work - DISTINCT can't be used in LISTAGG
SQL> select deptno, listagg(distinct job, ', ') within group (order by job) jobs
2 from emp
3 group by deptno;
select deptno, listagg(distinct job, ', ') within group (order by job) jobs
*
ERROR at line 1:
ORA-30482: DISTINCT option not allowed for this function
SQL>
SQL> -- So - select distinct jobs first, then apply LISTAGG to it
SQL> select x.deptno, listagg(x.job, ', ') within group (order by x.job) jobs
2 from (select distinct deptno, job
3 from emp) x
4 group by x.deptno;
DEPTNO JOBS
---------- ------------------------------------------------------------
10 CLERK, MANAGER, PRESIDENT
20 ANALYST, CLERK, MANAGER
30 CLERK, MANAGER, SALESMAN
SQL>
首先选择所需的不同值,然后对其应用LISTAGG。下面是一个基于Scott模式的示例
SQL> -- Duplicate jobs within the department
SQL> select deptno, listagg(job, ', ') within group (order by job) jobs
2 from emp
3 group by deptno;
DEPTNO JOBS
---------- ------------------------------------------------------------
10 CLERK, MANAGER, PRESIDENT
20 ANALYST, ANALYST, CLERK, CLERK, MANAGER
30 CLERK, MANAGER, SALESMAN, SALESMAN, SALESMAN, SALESMAN
SQL>
SQL> -- This won't work - DISTINCT can't be used in LISTAGG
SQL> select deptno, listagg(distinct job, ', ') within group (order by job) jobs
2 from emp
3 group by deptno;
select deptno, listagg(distinct job, ', ') within group (order by job) jobs
*
ERROR at line 1:
ORA-30482: DISTINCT option not allowed for this function
SQL>
SQL> -- So - select distinct jobs first, then apply LISTAGG to it
SQL> select x.deptno, listagg(x.job, ', ') within group (order by x.job) jobs
2 from (select distinct deptno, job
3 from emp) x
4 group by x.deptno;
DEPTNO JOBS
---------- ------------------------------------------------------------
10 CLERK, MANAGER, PRESIDENT
20 ANALYST, CLERK, MANAGER
30 CLERK, MANAGER, SALESMAN
SQL>
我试过了,但它说ORA-30482:这个函数30482不允许使用不同的选项。00000-“此函数不允许使用不同选项”,因为它仅适用于Oracle 19c。有关
listagg
的版本比较,请参见以下答案:我尝试过,但它显示ORA-30482:此函数30482不允许使用不同的选项。00000-“此函数不允许使用不同选项”,因为它仅适用于Oracle 19c。有关listag
的版本比较,请参见以下答案:您能提供示例数据吗?i、 你能提供样本数据吗?i、 e SqlFiddleof