Concatenate and group multiple rows in Oracle

Posted by user1693347 on Stack Overflow See other posts from Stack Overflow or by user1693347
Published on 2012-09-24T03:32:47Z Indexed on 2012/09/24 3:37 UTC
Read the original article Hit count: 85

Suppose I have a table like this:

NAME GROUP

name1 groupA

name2 groupB

name5 groupC

name4 groupA

name3 groupC

I'd like to have a result like this:

GROUP NAMES

groupA name1,name4

groupB name2

groupC name3,name5

If there were only one column in the table, I could concatenate the records by doing the following, but with grouping in the context, I really don't have much idea. Any suggestion is welcome, thanks in advance!

Concatatenating one column table:

SELECT names 
FROM (SELECT SYS_CONNECT_BY_PATH(names,' ') names, level
      FROM name_table

      START WITH names = (SELECT names FROM name_table WHERE rownum = 1)
      CONNECT BY PRIOR names < names
      ORDER BY level DESC)
      WHERE rownum = 1 

© Stack Overflow or respective owner

Related posts about sql

Related posts about Oracle