【问题标题】:Fetching value using LISTAGG but avoid grouping of a table used in Inner join使用 LISTAGG 获取值,但避免对内连接中使用的表进行分组
【发布时间】:2023-03-26 10:23:01
【问题描述】:

您好,我有一个类似的 Oracle 查询

select listagg(name,',') within group (order by name)
from table t1 
inner join table t2 on t1.id=t2.id
inner join table t3 on t1.value=t3.value

但后来我需要从另一个表 t4 中再获取 2 列,所以我加入了 t4,但是当我加入时,listagg 给出了用逗号分隔的重复值

select listagg(name,',') within group (order by name)
from table t1 
inner join table t2 on t1.id=t2.id
inner join table t3 on t1.value=t3.value
inner join table t4 on t1.id=t4.id

我想在不影响 LISTAGG 函数的情况下获取这两个新列。

【问题讨论】:

    标签: sql oracle oracle10g aggregate-functions string-aggregation


    【解决方案1】:

    通过添加连接,您会在结果中得到重复的名称。假设您的联接是正确的,您可以使用 DISTINCT 来解决此问题:

    select listagg(name,',') within group (order by name)
    from (select distinct name
          from table t1 
          inner join table t2 on t1.id=t2.id
          inner join table t3 on t1.value=t3.value
          inner join table t4 on t1.id=t4.id
    )
    

    【讨论】:

    • 这会在 listagg 字段中创建唯一值,但最终输出中的行会有重复行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多