是否有一个Oracle SQL查询将多行汇总为一行? [英] Is there an Oracle SQL query that aggregates multiple rows into one row?
问题描述
我有一个看起来像这样的表:
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.
这篇关于是否有一个Oracle SQL查询将多行汇总为一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!