表:
users
USER ACTIVITY_TYPE
---- ---------------
John 23L
Mike 159
activities
ACTIVITY_CODE DESCRIPTION
-------------------- --------------------
1 music
2 math
3 english
5 physics
9 french
L chemistry
询问:
select user_id, listagg(description, ',') within group (order by rk) as activities_list
from (select user_id, rk, description from users u join
(select rownum rk from all_tables) r on r.rk <= length(activity_type)
join activities a on a.activity_code = substr(u.activity_type, rk, 1) )
group by user_id
/
结果:
USER ACTIVITIES_LIST
---- --------------------------------------------------
John math,english,chemistry
Mike music,physics,french