【问题标题】:Remove Redundant Values in Cell built from LISTAGG, Oracle SQL [duplicate]删除从 LISTAGG、Oracle SQL 构建的单元格中的冗余值 [重复]
【发布时间】:2015-03-20 21:36:03
【问题描述】:

我对在 oracle 中查询非常陌生。我已经使用 LISTAGG 构建了一个 oracle 查询,如下所示:

select a.field1, 
LISTAGG(d.field2, ';') WITHIN GROUP (ORDER BY d.field2) AS FIELD_ALIAS
from
table1 a,  table2 b, 
table4 c, table5 d

where
a.field2 = b.field2
and
b.field2 = c.field2
and
c.field3 = d.field3

group by
a.field1

返回:

field1   field2
----------------
504482   Labour;Labour;Labour;Labour;Labour;Labour;Labour;Labour

我想做的是简化第二个字段并删除冗余值,以便我得到:

field1   field2
----------------
504482   Labour

这可能吗?

【问题讨论】:

    标签: sql oracle listagg


    【解决方案1】:

    我不认为listagg() 采用distinct 关键字。一种方法是使用子查询:

    select field1, LISTAGG(d.field2, ';') WITHIN GROUP (ORDER BY field2)
    from (select distinct a.field1, d.field2
          from table1 a join
               table2 b
               on a.field2 = b.field2 join
               table4 c
               on b.field2 = c.field2 join
               table5 d
               on c.field3 = d.field3
          ) t
    group by field1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 2018-12-03
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      相关资源
      最近更新 更多