Solved

How can I use listagg with distinct.

Posted on 2016-08-31
4
762 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

615 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