简体   繁体   English

具有内部联接的ORACLE Listagg

[英]ORACLE Listagg with an Inner Join

I need to pull data from two tables where two values match. 我需要从两个值匹配的两个表中提取数据。 The joining table produces 5 rows, and will always produce 5 rows. 联接表产生5行,并将始终产生5行。

Is there a way that I can take one column with distinct values and return multiple columns in a single row? 有没有一种方法可以使一列具有不同的值并在一行中返回多列?

For an example: Table A: orig_zip, dest_zip, pri_mode 例如:表A:orig_zip,dest_zip,pri_mode

Table B: orig_zip, dest_zip, serv_comm 表B:orig_zip,dest_zip,serv_comm

Table B will always return 5 results for every 1 result in Table A when doing an inner join like the following: 进行内部联接时,表B始终会在表A中的每1个结果中返回5个结果:

SELECT a.orig_zip, a.dest_zip, b.serv_comm, a.pri_mode
FROM A a
INNER JOIN B b
ON a.orig_zip = b.orig_zip and a.dest_zip = b.dest_zip
ORDER BY a.orig_zip, a.dest_zip, b.mail_class;

How can I take the 5 results, and turn them into a single row in oracle. 我怎样才能得到5个结果,并在oracle中将它们变成一行。 The only field that is different in the results will be the serv_comm field. 结果中唯一不同的字段是serv_comm字段。

The final row should have the following setup: 最后一行应具有以下设置:

[ORIG_ZIP][DEST_ZIP][SERV_COMM1][SERV_COMM2][SERV_COMM3][SERV_COMM4][SERV_COMM5][PRI_MODE] [ORIG_ZIP] [DEST_ZIP] [SERV_COMM1] [SERV_COMM2] [SERV_COMM3] [SERV_COMM4] [SERV_COMM5] [PRI_MODE]

I did some research, and here is the answer on SQL Fiddle! 我做了一些研究,这是SQL Fiddle的答案!

http://sqlfiddle.com/#!4/783b8/1/0 http://sqlfiddle.com/#!4/783b8/1/0

From 11g you can also do it with the PIVOT clause: 从11g开始,您还可以使用PIVOT子句进行操作:

FROM  trans_mode a
INNER JOIN servcomm b
ON a.orig_zip = b.orig_zip and a.dest_zip = b.dest_zip
pivot
(
  max(serv_comm)
  for (mail_class)
  in ('PRI' AS pri_serv, 'FCM' AS fcm_serv, 'PER' AS per_serv, 'PKG' AS pkg_serv, 'STD' AS std_serv)
  );

Here is a sqlfiddle demo (based on yours) 这是一个sqlfiddle演示(基于您的演示)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM