如何在不删除使用Oracle内部查询中使用的重复项的情况下获取选择值?

时间:2019-06-17 09:15:24

标签: sql oracle

我需要内部查询中产生重复项的结果集,截至目前,我将选择值作为内部查询中重复项的一个结果。

当前工作查询

select listagg(column3, '') within group (order by column3) 
from table_name
where column3 in ('ABA', 'ABC', 'ABC', 'ABD', 'ABF', 'ERF', 'AGR', 'LFS', 'BOE') and
      column1=384

将结果作为

ABAABCABFERFLFSBOE

根据表中可用的column3和column1条件,我将在同一行中打印column3的所有行值,而不会附加任何空格或多余字符

但是我需要实际结果

ABAABCABCABFERFLFSBOE

我需要两次将结果作为in查询中的重复项两次。任何人都可以在此查询中为我提供帮助,而无需通过打印删除重复项,因为条件为true且内部查询中存在重复项。

2 个答案:

答案 0 :(得分:0)

据我所知,查询您编写的发挥作用。

似乎其中column3 = 'ABC'没有column1 = 384的行之一导致ABC出现之一从结果中删除。

如何不删除那些重复项?也许通过使用联接?

SQL> with tn (col1, col3) as
  2    (select 384, 'ABC' from dual union all
  3     select 111, 'ABC' from dual union all
  4     select 384, 'FFF' from dual union all
  5     select 384, 'DDD' from dual
  6    )
  7  select listagg(a.col1, '') within group (order by null) result
  8  from tn a join tn b on a.col3 = b.col3
  9  where a.col3 in ('ABC', 'ABC', 'FFF')     --> simulating your IN list
 10    and b.col1 = 384;

RESULT
-----------------------------------------------------------------------------
111384384

SQL>

答案 1 :(得分:0)

为此,您需要一个left join,而不是in

select listagg(t.column3, '') within group (order by t.column3) 
from (select 'ABA' as val from dual union all
      select 'ABC' as val from dual union all
      select 'ABC' as val from dual union all
      select 'ABD' as val from dual union all
      select 'ABF' as val from dual union all
      select 'ERF' as val from dual union all
      select 'AGR' as val from dual union all
      select 'LFS' as val from dual union all
      select 'BOE' as val from dual
     ) v left join
     table_name t
     on v.val = t.column3
where v.column1 = 384;