Oracle 10g 多列字符串连接
是否可以构造 SQL 来连接多行的列值?
以下是一个示例:
表 A
PID A B C
表 B
PID SEQ Desc A 1 Have A 2 a nice A 3 day. B 1 Nice Work. C 1 Yes C 2 we can C 3 do C 4 this work!
SQL 的输出应该是 -
PID Desc A day.||a nice||Have B Nice Work. C this work!||do||we can||Yes
所以输出表的 Desc 列基本上是表 B 中 SEQ 值的串联,并且这些值按 SEQ 的降序顺序附加,并由|| ?
有 SQL 方面的帮助吗?
仅供参考 - 寻找不使用函数或存储过程的解决方案
Would it be possible to construct SQL to concatenate column values from multiple rows?
The following is an example:
Table A
PID A B C
Table B
PID SEQ Desc A 1 Have A 2 a nice A 3 day. B 1 Nice Work. C 1 Yes C 2 we can C 3 do C 4 this work!
Output of the SQL should be -
PID Desc A day.||a nice||Have B Nice Work. C this work!||do||we can||Yes
So basically the Desc column for output table is a concatenation of the SEQ values from Table B and the values are appended in the descending order of the SEQ and delimited by || ?
Any help with the SQL?
FYI - Looking for solution without using functions or stored procedures
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自这里
但我会去任何一天都有一个功能。
From here
But I'd go with a function any day.
分层查询应该可以工作。因为您想从每个 PID 的最高 SEQ 开始,所以需要一些额外的技巧。
编辑:根据评论中的要求添加过滤器的一种方法:
A hierarchical query should work. A little extra trickery is needed since you want to start with the highest SEQ for each PID.
Edit: One way to add a filter as requested in comment:
这里有很多关于如何做到这一点的示例(其中一些已经提到过),包括类似于 listagg() 的完整实现:
http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php#user_defined_aggregate_function
Here are a lot of examples on how to do it (some of them have been mentioned already) including a complete implementation of something similar to listagg():
http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php#user_defined_aggregate_function
您想在 Oracle 中执行
GROUP_CONCAT
在 MySQL 中执行的操作吗?如果存在的话,您可以使用
WM_CONCAT
:http://www.oracle-base.com/articles /misc/string-aggregation-techniques.php#wm_concat
但它没有记录,所以如果我是你,我不会在生产中使用它。
不幸的是,在 10g 上还没有
LISTAGG
。对于 10g 的生产环境,我会选择 Dave Costa 的答案。
You want to do something in Oracle what
GROUP_CONCAT
does in MySQL?You can use
WM_CONCAT
if it is present:http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php#wm_concat
But it is undocumented, so I wouldn't use that on production, if I were you.
On 10g there is no
LISTAGG
yet, unfortunately.For production environments on 10g, I would go with Dave Costa's answer.