[英]oracle distinct with listagg
I have a table. 我有桌子 I can show all the data of a colomn in my table using ',' in the same line. 我可以在同一行中使用“,”显示表中列的所有数据。 but I can't apply it distinctly. 但我不能明确地应用它。 hepl please 请帮忙
This is tricky. 这很棘手。 One simple suggestion is to use select distinct
: 一个简单的建议是使用select distinct
:
select listagg(col, ',') within group (order by col)
from (select distinct col from t) x;
However, that makes it difficult to calculate other aggregations (or to generate more than on listagg()
result). 但是,这使得难以计算其他聚合(或生成比listagg()
结果更多的聚合)。 Another way is to use window functions in combination with 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.