Oracle 10g 中的聚合字符串连接

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

Aggregate String Concatenation in Oracle 10g

sqloracleoracle10gaggregate-functions

提问by Arjun Vasudevan

I'm using Oracle 10 g, I have a scenario similar to this:

我正在使用 Oracle 10 g,我有一个类似的场景:

No Name
-- -----
1 Rony
1 James
1 Aby
2 Sam
2 Willy
3 Mike

没有名字
- -----
1罗尼
1詹姆斯
1奥比
2萨姆
2威利
3麦克

I need to aggregate and concatenate the strings (with a single space in between), in a way to get the results:

我需要聚合和连接字符串(中间有一个空格),以某种方式获得结果:

No Name
-- -----
1 Rony James Aby
2 Sam Willy
3 Mike

没有名字
- -----
1罗尼·詹姆斯·奥比
2萨姆威利
3麦克

I'm using Oracle 10g and have to implement this using SQL and not PL/SQL. Is there a way out?

我正在使用 Oracle 10g,并且必须使用 SQL 而不是 PL/SQL 来实现它。有出路吗?

回答by shonky linux user

It is easy on 11G, you can use the LISTAGG function, but sadly not on 10G

在 11G 上很容易,您可以使用 LISTAGG 功能,但遗憾的是在 10G 上不行

There are some techniques here for earlier versions however they do require a function to be written.

这里有一些适用于早期版本的技术,但是它们确实需要编写一个函数。

http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php

http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php

回答by Dinup Kandel

Try this query

试试这个查询

select No  , rtrim(Name,',') Name
   from ( select No , Name , rn from yourtable
           model
  partition by (No)
  dimension by (row_number() over
 (partition by No order by Name) rn
 )
 measures  (cast(Name as varchar2(40)) Name)
   rules
 ( Name[any] order by rn desc = Name[cv()]||' '||Name[cv()+1]
   )
  )
    where rn = 1
    order by NO

Here is your sql demo

这是你的 sql 演示

回答by chetan

you can use LISTAGG

你可以使用 LISTAGG

see demo here

在这里演示

回答by Msyma

Try this SQLquery

试试这个SQL查询

SELECT 
  [No],
  STUFF((
    SELECT ' ' + Name
    FROM #tbl_concat 
    WHERE ([No] = Results.[No]) 
    FOR XML PATH (''))
  ,1,0,'') AS NameValues
FROM #tbl_concat Results
GROUP BY [No]