我有以下数据集:
user_id | advr1_id | advr1_name | advr2_id | advr2_name | advr3_id | advr3_name
--------+----------+------------+----------+------------+----------+-----------
7 | 22 | John Smith | 15 | Jane Doe | 9 | Bill Clint
8 | 15 | Jane Doe | 10 | Kim Gol | null | null
我想创建另一个包含所有顾问名称的列advrs
(这将放在上面的记录中)。
user_id | advrs
--------+---------------------------------
7 | John Smith; Jane Doe; Bill Clint
8 | Jane Doe; Kim Gol
我可以用listagg()
实现这个目标,但速度太慢了...有没有我不知道的替代方法?
答案 0 :(得分:2)
这样的事情可能是:
select user_id,
replace(
listagg(
advr1_name
|| decode(advr2_name, null, '', '# ' || advr2_name)
|| decode(advr3_name, null, '', '# ' || advr3_name)
, '; '
) within group (order by advr1_name),
'#', ';'
)
from your_tab
group by user_id;
答案 1 :(得分:0)
如果您添加advr4...
或更多,则效率不高或效率不高,并且不完全可扩展,但这可能比listagg更快地完成工作:
ltrim(rtrim(replace(advr1_name||'; '||advr2_name||'; '||advr3_name, '; ; ', '; '), '; '),'; ')