SQL Query to concatenate column values from multiple rows in Oracle

前端 未结 10 1259
鱼传尺愫
鱼传尺愫 2020-11-21 07:12

Would it be possible to construct SQL to concatenate column values from multiple rows?

The following is an example:

Table A

PID
A
B
C

10条回答
  •  遥遥无期
    2020-11-21 07:14

    With SQL model clause:

    SQL> select pid
      2       , ltrim(sentence) sentence
      3    from ( select pid
      4                , seq
      5                , sentence
      6             from b
      7            model
      8                  partition by (pid)
      9                  dimension by (seq)
     10                  measures (descr,cast(null as varchar2(100)) as sentence)
     11                  ( sentence[any] order by seq desc
     12                    = descr[cv()] || ' ' || sentence[cv()+1]
     13                  )
     14         )
     15   where seq = 1
     16  /
    
    P SENTENCE
    - ---------------------------------------------------------------------------
    A Have a nice day
    B Nice Work.
    C Yes we can do this work!
    
    3 rows selected.
    

    I wrote about this here. And if you follow the link to the OTN-thread you will find some more, including a performance comparison.

提交回复
热议问题