将来自多个记录的数据组合到SQL中单个记录的多个列中

时间:2018-02-26 16:02:03

标签: sql oracle performance join merging-data

请在下面找到我的表结构和测试数据。

CREATE TABLE TX(
  TX_ID  VARCHAR2(10),
  DESCRIPTION  VARCHAR2(100),
  CONSTRAINT "PK_TX" PRIMARY KEY ("TX_ID")
);

CREATE TABLE PARTNER(
  TX_ID  VARCHAR2(10),
  PTNR_ID  VARCHAR2(10),
  PTNR_NAME VARCHAR2(50),
  PTNR_TYPE VARCHAR2(20),
  CONSTRAINT "PK_TX_PARTNER" PRIMARY KEY ("TX_ID", "PTNR_ID"),
  CONSTRAINT "FK_TX_PARTNER" FOREIGN KEY ("TX_ID") REFERENCES TX("TX_ID") ON DELETE CASCADE ENABLE
);

INSERT INTO TX VALUES('1','Transaction 1');
INSERT INTO TX VALUES('2','Transaction 2');
INSERT INTO TX VALUES('3','Transaction 3');
INSERT INTO TX VALUES('4','Transaction 4');
INSERT INTO TX VALUES('5','Transaction 5');

INSERT INTO PARTNER VALUES('1','P1', 'Partner 1', 'Exporter');
INSERT INTO PARTNER VALUES('1','P2', 'Partner 2', 'Importer');
INSERT INTO PARTNER VALUES('1','P3', 'Partner 3', 'Supplier');
INSERT INTO PARTNER VALUES('2','P1', 'Partner 1', 'Exporter');
INSERT INTO PARTNER VALUES('2','P2', 'Partner 2', 'Importer');

现在,我需要检查与每笔交易相关的合作伙伴详细信息,并将其显示在一条记录中。输出应该是这样的。

TX_ID | DESCRIPTION    | PTNR_ID_1 | PTNR_NAME_1 | PTNR_ID_2 | PTNR_NAME_2 | PTNR_ID_3 | PTNR_NAME_3
----------------------------------------------------------------------------------------------------
1     | Transaction 1  | P1        | Partner 1   | P2        | Partner 2   | P3        | Partner 3
2     | Transaction 2  | P1        | Partner 1   | P2        | Partner 2

使用以下SQL查询,我能够得到我正在寻找的结果。但我正在寻找一种更好的方法来实现这一目标。

SELECT 
  T.TX_ID AS "TRANSACTION ID",
  T.DESCRIPTION AS "DESCRIPTION", 
  P1.PTNR_ID AS "EXPORTER ID", 
  P1.PTNR_NAME AS "EXPORTER NAME",
  P2.PTNR_ID AS "IMPORTER ID", 
  P2.PTNR_NAME AS "IMPORTER NAME",
  P3.PTNR_ID AS "SUPPLIER ID", 
  P3.PTNR_NAME AS "SUPPLIER NAME"
FROM 
  TX T
  LEFT OUTER JOIN PARTNER P1 ON (T.TX_ID = P1.TX_ID) AND P1.PTNR_TYPE='Exporter'
  LEFT OUTER JOIN PARTNER P2 ON (T.TX_ID = P2.TX_ID) AND P2.PTNR_TYPE='Importer'
  LEFT OUTER JOIN PARTNER P3 ON (T.TX_ID = P3.TX_ID) AND P3.PTNR_TYPE='Supplier';

修改 我尝试了内部查询和案例组合,如下所述。它没有以所需的方式返回数据。我正在获取重复记录。对于不同的合作伙伴类型,产生了不同的记录我不确定如何合并这些结果。

SELECT DISTINCT TRANSACTION_ID, DESCRIPTION, EXPORTER_ID, EXPORTER_NAME, IMPORTER_ID, IMPORTER_NAME, SUPPLIER_ID, SUPPLIER_NAME
FROM (
        SELECT 
          T.TX_ID AS "TRANSACTION_ID",
          T.DESCRIPTION AS "DESCRIPTION", 
          (CASE WHEN P1.PTNR_TYPE = 'Exporter' THEN P1.PTNR_ID ELSE NULL END) AS "EXPORTER_ID", 
          (CASE WHEN P1.PTNR_TYPE = 'Exporter' THEN P1.PTNR_NAME ELSE NULL END) AS "EXPORTER_NAME",
          (CASE WHEN P1.PTNR_TYPE = 'Importer' THEN P1.PTNR_ID ELSE NULL END) AS "IMPORTER_ID", 
          (CASE WHEN P1.PTNR_TYPE = 'Importer' THEN P1.PTNR_NAME ELSE NULL END) AS "IMPORTER_NAME",
          (CASE WHEN P1.PTNR_TYPE = 'Supplier' THEN P1.PTNR_ID ELSE NULL END) AS "SUPPLIER_ID", 
          (CASE WHEN P1.PTNR_TYPE = 'Supplier' THEN P1.PTNR_NAME ELSE NULL END) AS "SUPPLIER_NAME"
        FROM 
          TX T
          INNER JOIN PARTNER P1 ON (T.TX_ID = P1.TX_ID) AND P1.PTNR_TYPE= ANY('Exporter', 'Importer', 'Supplier')
);

另外,我正在寻找Oracle SQL解决方案,而不是PL / SQL解决方案。我可以使用BEGIN ... END块而不是游标。

1 个答案:

答案 0 :(得分:0)

您可以用不同的方式编写它,尝试这种方法。请改用inner join

SELECT 
  T.TX_ID AS "TRANSACTION ID",
  T.DESCRIPTION AS "DESCRIPTION", 
  (select PTNR_ID from partner where PTNR_TYPE='Exporter' )AS "EXPORTER ID", 
  (select PTNR_NAME from partner where PTNR_TYPE='Exporter' ) AS "EXPORTER NAME",
  (select PTNR_ID from partner where PTNR_TYPE='Importer' ) AS "IMPORTER ID", 
  (select PTNR_NAME from partner where PTNR_TYPE='Importer' ) AS "IMPORTER NAME",
  (select PTNR_ID from partner where PTNR_TYPE='Supplier' ) AS "SUPPLIER ID", 
   (select PTNR_NAME from partner where PTNR_TYPE='Supplier' )    AS "SUPPLIER NAME"
FROM 
  TX T
  inner join partner p on p.tx_id = t.tx_id