Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

To remove duplicate records using XMLAGG() function

User_ZVLXKJun 14 2022

Hi,
Consider Table Product which consists of two columns with following records as shown below :
Product_ID Product_Name
001 IPhone
001 IPhone
001 Watch
001 Laptop
001 Laptop
Expected Output :
Product_ID Product_name
001 IPhone, Watch, Laptop
However, when I am trying to achieve output using XMLAGG() Function,
Command :
select rtrim(xmlagg(xmlelement(e,product_name,', ').extract('//text()') order by product_name).getclobval(),', ') "Product_Name" from Product;
Output Appearing using XMLAGG() Function :
Product_ID Product_Name
001 IPhone, IPhone, Watch, Laptop, Laptop
I need output which can eliminate IPhone and Laptop records appearing twice for same product. id "001".
Can you please advice how to fix this issue.
Thanks for understanding in advance.

Comments
Post Details
Added on Jun 14 2022
3 comments
1,801 views