Oracle: Combine multiple results in a subquery into a single comma-separated value [duplicate]

女生的网名这么多〃 提交于 2019-11-26 08:26:26

问题


I\'m trying to convert a single-columned subquery into a command-separated VARCHAR-typed list of values.

This is identical to this question, but for Oracle rather than SQL Server or MySQL.


回答1:


There is an excellent summary of the available string aggregation techniques on Tim Hall's site.




回答2:


I found this that seems to work. Thoughts?

SELECT SUBSTR (c, 2) concatenated
  FROM (SELECT     SYS_CONNECT_BY_PATH ( myfield, ',') c, r
              FROM (SELECT   ROWNUM ID, myfield,
                             RANK () OVER (ORDER BY ROWID DESC) r
                        FROM mytable
                    ORDER BY myfield)
        START WITH ID = 1
        CONNECT BY PRIOR ID = ID - 1)
 WHERE r = 1;



回答3:


11.2 introduced LISTAGG, which unlike WM_CONCAT is documented. We are not on 11.2 yet, so we use a custom aggregate function.




回答4:


Here's a blog that shows an Oracle query to work like MySQL's GROUP_CONCAT():

http://halisway.blogspot.com/2006/08/oracle-groupconcat-updated-again.html




回答5:


SELECT deptno, wm_concat(ename) AS employees FROM emp GROUP BY deptno;

Reference: http://forums.oracle.com/forums/thread.jspa?messageID=1186901&#1186901



来源:https://stackoverflow.com/questions/492563/oracle-combine-multiple-results-in-a-subquery-into-a-single-comma-separated-val

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!