Solved

How can I use listagg with distinct.

Posted on 2016-08-31
4
574 Views
Last Modified: 2016-09-01
Dear Experts,

My requirement is to get distinct values after I use listagg.
But distinct is not allowed inside listagg,

Please help !!
0
Comment
Question by:sakthikumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 41778145
simply wrap your source data in an inline view and make that distinct, then select listagg from that

for example...

select listagg(owner,',') within group(order by owner) from
     (select distinct owner from all_objects)
0
 
LVL 5

Expert Comment

by:Abhimanyu Suri
ID: 41778154
Can you please quote an example

If it is one row o/p combined with listagg, below example may help

SELECT LISTAGG (usr, ',') WITHIN GROUP (ORDER BY usr)
  FROM (SELECT DISTINCT username usr FROM USERS)
0
 

Author Comment

by:sakthikumar
ID: 41778221
the column already has comma separated values, so distinct is not working.

data is like below

category  email
a                email1,email2,email3
b               email2,email3,email6

when I do a group I should get only distinct email IDs
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 41778241
then you need to split your data first, then combine it afterward.


SELECT LISTAGG(email, ',') WITHIN GROUP (ORDER BY email)
  FROM (SELECT DISTINCT REGEXP_SUBSTR(
                            email,
                            '[^,]+',
                            1,
                            COLUMN_VALUE
                        )
                            email
          FROM yourdata,
               TABLE(
                       SELECT COLLECT(LEVEL)
                         FROM DUAL
                   CONNECT BY LEVEL <= REGEXP_COUNT(email, '[^,]+')
               ))

Open in new window

1

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question