SQL Query to concatenate column values from multiple rows in Oracle

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

    1. LISTAGG delivers the best performance if sorting is a must(00:00:05.85)

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

    2. COLLECT delivers the best performance if sorting is not needed(00:00:02.90):

      SELECT pid, TO_STRING(CAST(COLLECT(Desc) AS varchar2_ntt)) AS Vals FROM B GROUP BY pid;

    3. COLLECT with ordering is bit slower(00:00:07.08):

      SELECT pid, TO_STRING(CAST(COLLECT(Desc ORDER BY Desc) AS varchar2_ntt)) AS Vals FROM B GROUP BY pid;

    All other techniques were slower.

提交回复
热议问题