0

Let's say I have a N:N table relationship, and i'm doing a join between them, the problem is that the result have many rows with the same values, and i want to resume in one row containing the ids of those.
The result that actually returned look like this.

    ID_STREET     STREET      COUNTRY       REGION    
    ------------------------------------------------
    1             street-1    country-1     region-1     
    2             street-1    country-1     region-1
    3             street-2    country-2     region-2  

and i want to get it as

        ID_STREET     STREET      COUNTRY       REGION   DUPLICATED 
    ---------------------------------------------------------------
    1             street-1    country-1     region-1     1,2
    3             street-2    country-2     region-2     3

Table definition look like

-------------------------         --------------------------
        STREET                            COUNTRY
-------------------------         --------------------------
ID   NUMBER NOT NULL              ID   NUMBER NOT NULL
NAME VARCHAR2(45) NOT NULL        NAME VARCHAR2(45) NOT NULL
ID_COUNTRY NUMBER NOT NULL         ID_REGION NUMBER NOT NULL
...other fields                   ...other fields

-------------------------         
        REGION                            
-------------------------         
ID   NUMBER NOT NULL              
NAME VARCHAR2(45) NOT NULL        
...other fields    
1
  • Can you post your two tables? The results are kind of confusing without seeing what you are starting from. Commented Oct 18, 2013 at 17:33

1 Answer 1

2

On oracle, the aggregate LISTAGG function can do string concatenation on a column grouped by other columns.

http://docs.oracle.com/cd/E11882_01/server.112/e10592/functions089.htm

From what I can gather from your table, if we did this to your first set of results

SELECT MIN(ID_STREET), STREET, COUNTRY, REGION, 
       LISTAGG(ID_STREET, ',') WITHIN GROUP (ORDER BY STREET) AS DUPLICATED
  FROM MYTABLE
GROUP BY STREET,COUNTRY,REGION

We'd end up with your second. I would need to see the original tables to show a query that would work to do what you want to do.

Sign up to request clarification or add additional context in comments.

2 Comments

Only on Oracle 11g or later.
WM_CONCAT could be used on earlier versions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.