oracle 如何在oracle中使用COALESCE合并两行数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1614373/
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-18 19:16:49  来源:igfitidea点击:

how to use COALESCE in oracle to combine data from two rows

oracle

提问by Omnipresent

I have the following query which is inside a loop and is breaking

我有以下查询,它在循环内并且正在中断

SELECT   COALESCE(v_user_grade || ', ', '') || user_grade
          INTO v_user_grade
          FROM EXPIRATION_HISTORY__2
           WHERE hist_id = v_test;

So basically in each iteration of the loop we get a new value for v_test. In some cases this query would return multiple rows. that is when it is breaking.

所以基本上在循环的每次迭代中,我们都会为 v_test 获得一个新值。在某些情况下,此查询将返回多行。那是它破裂的时候。

What I want is:

我想要的是:

Data in EXPIRATION_HISTORY__2:

EXPIRATION_HISTORY__2 中的数据:

hist_id    user_grade
301     ADMIN
302     ADMIN
302     USER

so now based on the above data. ...in the end of the iteration where v_test = 302. I would want v_user_gradeto contain (ADMIN, USER)

所以现在基于上述数据。...在迭代结束的地方v_test = 302。我想v_user_grade包含 ( ADMIN, USER)

回答by pyrocumulus

I think the following link describes exactly that what you want:

我认为以下链接准确描述了您想要的内容:

http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php

http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php

String Aggregation Techniques

On occasion it is necessary to aggregate data from a number of rows into a single row, giving a list of data associated with a specific value.

字符串聚合技术

有时,需要将多行中的数据聚合到一行中,从而给出与特定值相关联的数据列表。

I'm not an Oracle expert; but there are multiple solutions, depending on your DB enviroment.

我不是 Oracle 专家;但是有多种解决方案,具体取决于您的数据库环境。