SQL Oracle:将子查询中的多个结果合并为一个逗号分隔的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/492563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:55:50  来源:igfitidea点击:

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

sqloraclesubquerystring-aggregation

提问by Jason Cohen

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

我正在尝试将单列子查询转换为命令分隔VARCHAR类型的值列表。

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

这与此问题相同,但适用于 Oracle 而不是 SQL Server 或 MySQL。

回答by Justin Cave

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

Tim Hall 的站点上对可用的字符串聚合技术进行了很好的总结。

回答by Leigh Riffel

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

11.2 引入了LISTAGG,与 WM_CONCAT 不同,它被记录在案。我们还没有使用 11.2,因此我们使用自定义聚合函数。

回答by Jason Cohen

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;

回答by Bill Karwin

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

这是一个博客,它显示了一个 Oracle 查询像 MySQL 一样工作GROUP_CONCAT()

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

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

回答by bluwater2001

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

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

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

参考:http: //forums.oracle.com/forums/thread.jspa?messageID=1186901᱕