我在SQL查询中使用了WM_CONCAT
。数据库已更新至版本12c,WM_CONCAT
不起作用。
查询具有价值:
Select r.product_id, count (1) how_many, wm_concat (distinct r.number) numbers From ...
如何将其转换为LISTAGG
函数。我尝试了这个,但是没用:
Select r.product_id, count(1) how_many, LISTAGG(r.number, ',') WITHIN GROUP (order by r.number) numbers From ...
答案 0 :(得分:3)
您应该在查询中添加分组依据
with tab as(
select '10' as num, 'A' as product from dual union all
select '10' as num, 'B' as product from dual union all
select '10' as num, 'C' as product from dual union all
select '10' as num, 'D' as product from dual union all
select '20' as num, 'A' as product from dual union all
select '20' as num, 'B' as product from dual union all
select '30' as num, 'A' as product from dual
)
select product
, count (1) how_many
, LISTAGG(num, ',') WITHIN GROUP (order by num)
from tab
group by product
但您必须小心,因为使用listagg
只能连接最大4K的字符串。还有其他字符串聚合技术,请看答案here。
PRODUCT | HOW_MANY | LISTAGG(NUM,',')WITHINGROUP(ORDERBYNUM) :------ | -------: | :-------------------------------------- A | 3 | 10,20,30 B | 2 | 10,20 C | 1 | 10 D | 1 | 10
db <>提琴here