【问题标题】:How to eliminate repited values from result of Oracle LISTAGG aggregated query [duplicate]如何从 Oracle LISTAGG 聚合查询的结果中消除重复值 [重复]
【发布时间】:2020-04-19 08:01:14
【问题描述】:

我正在尝试消除重复的价值观 来自this Oracle's recomendation 的汇总结果:

with depts as (
select 'ACCOUNTING' as department , 'CLERC' as job from dual union all
select 'ACCOUNTING' as department , 'MANAGER' as job from dual union all
select 'ACCOUNTING' as department , 'PRESIDENT' as job from dual union all

select 'RESEARCH' as department , 'ANALYST' as job from dual union all
select 'RESEARCH' as department , 'ANALYST' as job from dual union all
select 'RESEARCH' as department , 'CLERK' as job from dual union all
select 'RESEARCH' as department , 'CLERK' as job from dual union all
select 'RESEARCH' as department , 'MANAGER' as job from dual union all

select 'SALES' as department , 'MANAGER' as job from dual union all
select 'SALES' as department , 'CLERK' as job from dual union all
select 'SALES' as department , 'SALESMAN' as job from dual union all
select 'SALES' as department , 'SALESMAN' as job from dual union all
select 'SALES' as department , 'SALESMAN' as job from dual union all
select 'SALES' as department , 'SALESMAN' as job from dual
)

select d.department,
       listagg (d.job,', ' ) within group (order by d.job) jobs
  from depts d
 group by d.department

正如您所见,外业工作包含重复的价值。

Oracle 建议以这种方式消除这种情况:

with depts as (
select 'ACCOUNTING' as department , 'CLERC' as job from dual union all
select 'ACCOUNTING' as department , 'MANAGER' as job from dual union all
select 'ACCOUNTING' as department , 'PRESIDENT' as job from dual union all

select 'RESEARCH' as department , 'ANALYST' as job from dual union all
select 'RESEARCH' as department , 'ANALYST' as job from dual union all
select 'RESEARCH' as department , 'CLERK' as job from dual union all
select 'RESEARCH' as department , 'CLERK' as job from dual union all
select 'RESEARCH' as department , 'MANAGER' as job from dual union all

select 'SALES' as department , 'MANAGER' as job from dual union all
select 'SALES' as department , 'CLERK' as job from dual union all
select 'SALES' as department , 'SALESMAN' as job from dual union all
select 'SALES' as department , 'SALESMAN' as job from dual union all
select 'SALES' as department , 'SALESMAN' as job from dual union all
select 'SALES' as department , 'SALESMAN' as job from dual
)

SELECT d.department,
       (select LISTAGG(job,', ')
               WITHIN GROUP (ORDER BY job)
          from (select unique job job
                  from depts t
                 where t.department = d.department)) jobs
  FROM depts d

但它不适用于版本。 Oracle Database 11g 企业版 11.2.0.4.0 - 64 位。

这个建议正确吗? 据我所知,我们无法将数据发送到嵌套查询到第二级嵌套。

【问题讨论】:

    标签: sql string oracle group-by listagg


    【解决方案1】:

    启动Oracle 19c,LISTAGG()支持DISTINCT

    select department, listagg(distinct job, ',') within group(order by job) jobs
    from depts
    group by department 
    

    在早期版本中,典型的解决方法是在子查询中 SELECT DISTINCT,然后聚合:

    select department, listagg(job, ',') within group(order by job) jobs
    from (select distinct department, job from depts) t
    group by department
    

    Demo on DB Fiddle

    部门 |工作 :--------- | :------------------------ 会计 |文员、经理、总裁 研究 |分析师、文员、经理 销售 |文员、经理、推销员

    【讨论】:

    • 那么,Oracle 网站上的建议是否正确?
    【解决方案2】:

    另一种可能性是部署LISTAGG 忽略NULL 值的功能。

    简单的row_number the duplicates andlistaggonly the values withrn = 1`

    with dep as (
    select 
      DEPARTMENT, JOB,
      row_number() over (partition by  DEPARTMENT, JOB order by JOB) as rn
    from depts)
    select 
     department, listagg(case when rn = 1 then job end, ',') within group(order by job) jobs
    from dep
    group by department
    

    【讨论】:

      猜你喜欢
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 2021-11-08
      • 2022-11-04
      相关资源
      最近更新 更多