SQL Query to concatenate column values from multiple rows in Oracle

前端 未结 10 1249
鱼传尺愫
鱼传尺愫 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:21

    There are a few ways depending on what version you have - see the oracle documentation on string aggregation techniques. A very common one is to use LISTAGG:

    SELECT pid, LISTAGG(Desc, ' ') WITHIN GROUP (ORDER BY seq) AS description
    FROM B GROUP BY pid;
    

    Then join to A to pick out the pids you want.

    Note: Out of the box, LISTAGG only works correctly with VARCHAR2 columns.

提交回复
热议问题