Solved

How can I use listagg with distinct.

Posted on 2016-08-31
4
75 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
  • 2
4 Comments
 
LVL 73

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 4

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 73

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
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

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now