【问题标题】:oracle distinct with listaggoracle 与 listagg 不同
【发布时间】:2018-09-21 13:05:46
【问题描述】:

我有一张桌子。我可以在同一行中使用“,”在我的表中显示一个列的所有数据。但我不能清楚地应用它。请帮忙

【问题讨论】:

  • 阅读stackoverflow.com/help/how-to-ask 部分“帮助其他人重现问题”.. 我投票关闭这个问题不清楚。
  • 很遗憾他们在介绍LISTAGG 时错过了DISTINCT 选项。这经常被抱怨。最后,Oracle 做出了反应:LISTAGG 将在版本 19c 中提供DISTINCT。见这里:community.oracle.com/ideas/12533
  • 有一个技巧可以在listagg 结果上使用regexp_replace 来删除重复项。您可以谷歌它,它也在上面的链接中提到。但是,listagg 字符串可能会变得非常大,然后您才能干扰并导致异常。我必须承认,当我想要不同的列表时,我经常使用wm_concat

标签: sql oracle distinct listagg


【解决方案1】:

这很棘手。一个简单的建议是使用select distinct

select listagg(col, ',') within group (order by col)
from (select distinct col from t) x;

但是,这使得计算其他聚合变得困难(或生成超过listagg() 结果)。另一种方法是结合listagg()使用窗口函数:

select listagg(case when seqnum = 1 then col end, ',') within group (order by col)
from (select t.*,
             row_number() over (partition by col order by col) as seqnum
      from t
     ) t

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2021-03-09
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    相关资源
    最近更新 更多