I have the following code, which groups emails for each customer that appears to be working fine except for a small caveat where there is a trailing comma at the end of each line, which I want to exclude.
I tried using regexp_replace with the following argument
regexp_replace(str, '.$') but I can't get past a syntax error.
Any help would be greatly appreciated. I am also open to any other solutions that can solve the problem.
select customer_id, listagg(email_id || ' , ') within group (order by customer_id) FROM (
select 1 as customer_id, 'hk@gmail.com' as email_id , 'primary' as usagetype from dual UNION
select 1 as customer_id, 'hk@tmail.com' as email_id , 'work' as usagetype from dual UNION
select 2 as customer_id, 'tt@tmail.com' as email_id , 'work' as usagetype from dual
)group by customer_id;