SQL查询到来自Oracle中多个行的连接列值
是否可以从 多行?
以下是一个示例:
表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 Have a nice day. B Nice Work. C Yes we can do this work!
输出应为 -因此,基本上,用于OUT PUT表的desc列是从表B中对SEQ值的串联?
SQL有帮助吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
有几种方式,具体取决于您拥有的版本 - 请参阅。一个非常常见的是使用 :
然后加入
a
以挑选您想要的pids
。注意:开箱即用,
listAgg
仅与varchar2
列正确使用。There are a few ways depending on what version you have - see the oracle documentation on string aggregation techniques. A very common one is to use
LISTAGG
:Then join to
A
to pick out thepids
you want.Note: Out of the box,
LISTAGG
only works correctly withVARCHAR2
columns.还有一个
xmlagg
函数,该功能可在11.2之前的版本上使用。因为wm_concat
是无证件且无证件且无需由Oracle置于Oracle 它在生产系统中。使用
xmlagg
您可以执行以下操作:从
雇用> lighteee_names
表中放置ename
column的值(与逗号串联)的值在XML元素(带有标签e)中There's also an
XMLAGG
function, which works on versions prior to 11.2. BecauseWM_CONCAT
is undocumented and unsupported by Oracle, it's recommended not to use it in production system.With
XMLAGG
you can do the following:What this does is
ename
column (concatenated with a comma) from theemployee_names
table in an xml element (with tag E)带有SQL模型子句:
我写了有关此在这里。而且,如果您遵循指向OTN-thread的链接,您会发现更多内容,包括性能比较。
With SQL model clause:
I wrote about this here. And if you follow the link to the OTN-thread you will find some more, including a performance comparison.
listAgg 分析功能是在 oracle 11g版本2 中引入的,这使得汇总字符串非常容易。
如果您使用的是11G版本2,则应将此功能用于字符串聚合。
有关字符串串联的更多信息,请参阅下面的URL。
http://www.oracle-base.com/articles/articles/articles/misc/misc/misc/stringggregatigatiquactechniques.php
string confenation
The LISTAGG analytic function was introduced in Oracle 11g Release 2, making it very easy to aggregate strings.
If you are using 11g Release 2 you should use this function for string aggregation.
Please refer below url for more information about string concatenation.
http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php
String Concatenation
正如大多数答案所暗示的那样,
listAgg
是显而易见的选项。但是,listAgg
的一个令人讨厌的方面是,如果串联字符串的总长度超过4000个字符(varchar2
in SQL中的限制),则会抛出以下错误,这很难在Oracle版本中管理至12.112cr2中添加的新功能是 listagg 的子句。
包括此子句在内的查询看起来像:
以上将将输出限制为4000个字符,但不会抛出
ora-01489
错误。这些是
on vollflflow
子句:上的
的其他选项:
'contd ..'
在字符串的结尾(默认为
...
)在溢出截断上''
:这将显示4000个字符没有任何终止字符串。
在用计数截断上溢出
:这将显示总计终止字符后末尾的字符数。
例如: - '
...(5512)
'listagg
在ORA-01489
错误(无论如何默认)。As most of the answers suggest,
LISTAGG
is the obvious option. However, one annoying aspect withLISTAGG
is that if the total length of concatenated string exceeds 4000 characters( limit forVARCHAR2
in SQL ), the below error is thrown, which is difficult to manage in Oracle versions upto 12.1A new feature added in 12cR2 is the
ON OVERFLOW
clause ofLISTAGG
.The query including this clause would look like:
The above will restrict the output to 4000 characters but will not throw the
ORA-01489
error.These are some of the additional options of
ON OVERFLOW
clause:ON OVERFLOW TRUNCATE 'Contd..'
: This will display'Contd..'
atthe end of string (Default is
...
)ON OVERFLOW TRUNCATE ''
: This will display the 4000 characterswithout any terminating string.
ON OVERFLOW TRUNCATE WITH COUNT
: This will display the totalnumber of characters at the end after the terminating characters.
Eg:- '
...(5512)
'ON OVERFLOW ERROR
: If you expect theLISTAGG
to fail with theORA-01489
error ( Which is default anyway ).对于那些必须使用Oracle 9i(或更早)解决此问题的人,您可能需要使用sys_connect_by_path,因为ListAgg不可用。
为了回答OP,以下查询将显示从表A中显示PID,并从表B中串联所有DESC列:
也可能有一些实例,其中键和值都包含在一个表中。可以在没有表A的情况下使用以下查询,并且仅存在表B:
所有值都可以根据需要重新排序。可以按子句中的分区中重新排序个体的串联描述,并且可以按子句以最终顺序重新排序PID列表。
替代:有时您想将整个表格中的所有值串联成一行。
这里的关键思想是使用人为的价值来串联描述组。
在下面的查询中,使用常数字符串'1',但是任何值都可以使用:
可以通过子句重新排序单个串联描述。
此页面上的其他几个答案也提到了以下非常有用的参考:
https://oracle-base.com/articles.com/articles/misc/misc/misc/string-string-string-string-aggregation-techniques
For those who must solve this problem using Oracle 9i (or earlier), you will probably need to use SYS_CONNECT_BY_PATH, since LISTAGG is not available.
To answer the OP, the following query will display the PID from Table A and concatenate all the DESC columns from Table B:
There may also be instances where keys and values are all contained in one table. The following query can be used where there is no Table A, and only Table B exists:
All values can be reordered as desired. Individual concatenated descriptions can be reordered in the PARTITION BY clause, and the list of PIDs can be reordered in the final ORDER BY clause.
Alternately: there may be times when you want to concatenate all the values from an entire table into one row.
The key idea here is using an artificial value for the group of descriptions to be concatenated.
In the following query, the constant string '1' is used, but any value will work:
Individual concatenated descriptions can be reordered in the PARTITION BY clause.
Several other answers on this page have also mentioned this extremely helpful reference:
https://oracle-base.com/articles/misc/string-aggregation-techniques
Listagg如果排序为必须(00:00:05.85)
,则提供最佳性能
在组中选择PID,Listagg(desc,'')作为描述
从B组;
收集如果不需要排序(00:00:02.90):
选择pid,to_string(cast(collect(desc)作为varchar2_ntt))作为b组的val;
用订购收集的vals vals b grout siter lit litlow show(00:00:07.088) ):
选择pid,to_string(cast(cast(desc of desc)作为varchar2_ntt collect(DESC订单))作为b group by b groud pid;
所有其他技术都较慢。
LISTAGG delivers the best performance if sorting is a must(00:00:05.85)
SELECT pid, LISTAGG(Desc, ' ') WITHIN GROUP (ORDER BY seq) AS description
FROM B GROUP BY pid;
COLLECT delivers the best performance if sorting is not needed(00:00:02.90):
SELECT pid, TO_STRING(CAST(COLLECT(Desc) AS varchar2_ntt)) AS Vals FROM B GROUP BY pid;
COLLECT with ordering is bit slower(00:00:07.08):
SELECT pid, TO_STRING(CAST(COLLECT(Desc ORDER BY Desc) AS varchar2_ntt)) AS Vals FROM B GROUP BY pid;
All other techniques were slower.
在运行选择查询之前,请运行以下操作:
在size 6000
上设置serveroutBefore you run a select query, run this:
SET SERVEROUT ON SIZE 6000
尝试此代码:
Try this code:
在选择您想要的位置的位置,请调用SQL函数。
例如:
对于SQL函数:
函数标头语法可能是错误的,但是原理确实有效。
In the select where you want your concatenation, call a SQL function.
For example:
Then for the SQL function:
The Function Header syntax might be wrong, but the principle does work.