Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004
    Location
    Rome, Italy
    Posts
    81

    Unanswered: concatenate many rows in a single row

    hi everybody,
    maybe the question I am posing is a nonsense or very easy to ask, but anyway I want to know the answer because I need it. Supose I have one table like this:
    col1 col2
    ---- ----------
    1 "NAME1-1"
    1 "NAME1-2"
    2 "NAME2-1"
    2 "NAME2-2"
    2 "NAME2-3"
    3 "NAME3-1"

    And I want the result from the query like this:
    col1 coln
    ---- --------------
    1 "NAME1-1NAME1-2"
    2 "NAME2-1NAME2-2NAME2-3"
    3 "NAME3-1"

    Thank you in advance,
    ducasio

  2. #2
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1
    It can be done, but it isn't trivial: Ask Tom

  3. #3
    Join Date
    Aug 2004
    Location
    Rome, Italy
    Posts
    81

    thanks

    Thank you very much andrewst,
    it was very useful to me and it work great.

    Bye, bye,
    ducasio

  4. #4
    Join Date
    Sep 2004
    Location
    London, UK
    Posts
    565
    There is an even less trivial way:
    Code:
    SELECT col1
         , MAX(SUBSTR(SYS_CONNECT_BY_PATH(col2,','),2))
           KEEP (DENSE_RANK LAST ORDER BY curr) AS names
    FROM   ( SELECT col1
                  , col2
                  , row_number() OVER (PARTITION BY col1 ORDER BY col2) AS curr
                  , NULLIF(row_number() OVER (PARTITION BY col1 ORDER BY col2) -1,0) AS prev
             FROM   test_tab )
    GROUP BY col1
    CONNECT BY prev = PRIOR curr AND col1 = PRIOR col1
    START WITH curr = 1;

  5. #5
    Join Date
    Jul 2003
    Posts
    2,296
    the get-transposed function is a great tool.
    easy to edit and apply to your own needs.
    - The_Duck
    you can lead someone to something but they will never learn anything ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •