SQL Query to concatenate column values from multiple rows in Oracle

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

    There's also an XMLAGG function, which works on versions prior to 11.2. Because WM_CONCAT is undocumented and unsupported by Oracle, it's recommended not to use it in production system.

    With XMLAGG you can do the following:

    SELECT XMLAGG(XMLELEMENT(E,ename||',')).EXTRACT('//text()') "Result" 
    FROM employee_names
    

    What this does is

    • put the values of the ename column (concatenated with a comma) from the employee_names table in an xml element (with tag E)
    • extract the text of this
    • aggregate the xml (concatenate it)
    • call the resulting column "Result"

提交回复
热议问题