0

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

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 块,但不能使用游标。

4

2 回答 2

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
于 2018-02-26T16:20:37.410 回答
0

如果您可以将合作伙伴信息作为单个列而不是单独的列,请尝试使用名为 LISTAGG 的 Oracle“分析函数”:

select a.tx_id,
       a.description,
       sub.result
  from tx a,
       (select b.tx_id,
               listagg('<' || b.ptnr_id || ' ' || b.ptnr_name || '>', ' ')
                within group (order by b.ptnr_id) as result
          from partner b
          group by b.tx_id
         ) sub
 where a.tx_id = sub.tx_id
于 2018-02-27T09:37:16.370 回答