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!

Need to add DISTINCT to SELECT LISTAGG CASE query

User_H76VHJul 20 2016 — edited Jul 20 2016

HI,

First, I want to say I am not a strong sql developer but I am learning.

I am writing a ORACLE select statement with LISTAGG. It works but when I was validating the data, I saw duplicates like 1:1,1:1,2:2,2:2.

I verified that the database do have duplicate. The query is for two tables in a one to many relationship. Below is a snippet of my query but I changed the column names etc..

how can I modified the following query to return unique values for each person?

I hope to achieve results like

1  John M. Doe 1:1, 2:2, 3:3, 4:4

NOT this

1 John M. Doe 1:1, 1:1, 2:2, 2:2, 3:3

I was told to add a DISTINCT to a subquery but  not sure how to achieve this. Any help is greatly appreciated. Thanks.

Thanks


    SELECT Table1.PERSON_ID,
           Table1.FIRST_NAME,
           Table1.MIDDLE_NAME,
           Table1.LAST_NAME,
   
    LISTAGG(case 
            when Table2.DESCRIPTION = '1' then '1:1'
            when Table2.DESCRIPTION = '2' then '2:2'
            when Table2.DESCRIPTION = '3' then '3:3'
            when Table2.DESCRIPTION = '4' then '4:4'
            when Table2.DESCRIPTION = '5' then '5:5'
            when Table2.DESCRIPTION = '6' then '6:6'
            when Table2.DESCRIPTION = '7' then '7:7'
            when Table2.DESCRIPTION = '8' then '8:8'
            when Table2.DESCRIPTION = '9' then '9:9'
    
   
          else ''
   
     end, ',') 
    WITHIN GROUP (ORDER BY Table2.DESCRIPTION ) 
   
    FROM Table1
    LEFT JOIN Table2
   
    ON Table2.PERSON_ID = Table1.PERSON_ID
    GROUP BY Table1.PERSON_ID,Table1.FIRST_NAME,Table1.MIDDLE_NAME, Table1.LAST_NAME

This post has been answered by JonWat on Jul 20 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 17 2016
Added on Jul 20 2016
3 comments
2,443 views