Solved

How can I use listagg with distinct.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

820 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