是否有将多行聚合为一行的 Oracle SQL 查询? [英] Is there an Oracle SQL query that aggregates multiple rows into one row?

查看:38
本文介绍了是否有将多行聚合为一行的 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.

相关问题:

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆