是否有将多行聚合为一行的 Oracle SQL 查询?

我有一张看起来像这样的表格:

I have a table that looks like this:

A 1 
A 2 
B 1 
B 2

我想生成一个如下所示的结果集:

And I want to produce a result set that looks like this:

A 1 2 
B 1 2

是否有可以执行此操作的 SQL 语句?我正在使用 Oracle.

Is there a SQL statement that will do this? I am using Oracle.

相关问题:

  • 从单行返回多行我的问题与这个问题的反面很接近.
  • 使用 LINQ 连接 这正是我想要做的,但没有 LINQ.
  • Returning multiple rows from a single row My question is close to the opposite of this question.
  • Use LINQ to concatenate This is exactly what I want to do, but without LINQ.

推荐答案

这取决于您使用的 Oracle 版本.如果它支持 wm_concat() 函数,那么你可以简单地做这样的事情:

It depends on the version of Oracle you're using. If it supports the wm_concat() function, then you can simply do something like this:

SELECT field1, wm_concat(field2) FROM YourTable GROUP BY field2;

wm_concat() 基本上就像 group_concat() 在 MySQL 中.它可能没有记录,所以启动你的旧 sqlplus 看看它是否在那里.

wm_concat() basically works just like group_concat() in MySQL. It may not be documented, so fire up ye olde sqlplus and see if it's there.

如果它不存在,那么您需要自己实现一些等效的东西.您可以在字符串聚合页面中找到有关如何执行此操作的说明在 oracle-base.com.

If it isn't there, then you'll want to implement something equivalent yourself. You can find some instructions on how to do this in the string aggregation page at oracle-base.com.

相关文章