1

我对在 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

那可能吗?

4

1 回答 1

2

我不认为这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
于 2015-03-20T21:42:39.020 回答