Example
select userid,company_name,remarks from visit_details where userid=786;
USERID COMPANY_NAME REMARKS
---------- ------------------------------ ---------------------------------------
786 abc xyz
786 def uvw
786 ghi pqr
The output requirement is
786 abc,def,ghi xyz,uvw,pqr
Solution
select userid,
(
select listagg(company_name,',') within group (order by company_name)
from visit_details where userid=786
) as company_name,
(
select listagg(remarks,',') within group (order by remarks)
from visit_details where userid=786
) as remarks
from
(
select userid,collect(distinct company_name),collect(distinct remarks)
from visit_details where userid=786 group by userid
) ;
USERID COMPANY_NAME REMARKS
---------- ------------------------------ ---------------------------------------
786 abc,def,ghi xyz,uvw,pqr
2) On single column
select listagg(company_name,',') within group (order by company_name)
from visit_details where userid=786 ;
COMPANY_NAME
----------
abc,def,ghi
select userid,company_name,remarks from visit_details where userid=786;
USERID COMPANY_NAME REMARKS
---------- ------------------------------ ---------------------------------------
786 abc xyz
786 def uvw
786 ghi pqr
The output requirement is
786 abc,def,ghi xyz,uvw,pqr
Solution
select userid,
(
select listagg(company_name,',') within group (order by company_name)
from visit_details where userid=786
) as company_name,
(
select listagg(remarks,',') within group (order by remarks)
from visit_details where userid=786
) as remarks
from
(
select userid,collect(distinct company_name),collect(distinct remarks)
from visit_details where userid=786 group by userid
) ;
USERID COMPANY_NAME REMARKS
---------- ------------------------------ ---------------------------------------
786 abc,def,ghi xyz,uvw,pqr
2) On single column
select listagg(company_name,',') within group (order by company_name)
from visit_details where userid=786 ;
COMPANY_NAME
----------
abc,def,ghi