简体   繁体   中英

Combination of INNER JOIN and SELECT

Suppose I have a table transactions with reported financial transactions where both sides of a transaction report ( TRNS_ID is transaction id, RA_ID is reporting agent id, CNTP_ID is counterparty id)

ID, RA_ID, CNTP_ID, FIELD1, FIELD2
1        A      B        0.1     0.1    
2        B      A        0.1     0.1
---------------------------------------
3        A      B        0.1     0.1    
4        B      A        0.1     0.1
---------------------------------------
5        B      C        0.1     0.1
6        C      B        0.1     0.1

Thus, for each transaction taking place between two agents, I have one redundant record and I want to get rid of it (for exposition purposes, I separated transactions belonging together by lines). The problem is to identify both transactions out of many records, possibly involving the same agents.

To do so, I perform a self join to match transactions with each other using INNER JOIN and a couple of criteria based on FIELD1 on FIELD2 . However, the information may not be enough for a perfect match (see first four records). Hence, I want to match on another field: the reported underlying collateral (possibly multiple items). Reported collaterals are stored in table collaterals where TRNS_ID is the foreign key

ID, COLL, TRNS_ID
1   F     1
2   G     1
3   G     2
4   F     2
-----------
5   H     3
6   H     4
-----------
7   I     5
8   I     6

Question: How can I impose the condition that the collateral items reported by both sides of the transaction are identical in the INNER JOIN query?

My desired result is

RA_TRNS_ID, RA_ID, CNTP_ID, CNTP_TRNS_ID, FIELD1, FIELD2
1           A      B        2             0.1     0.1
3           B      C        4             0.1     0.1
5           B      C        6             0.1     0.1

My curret attempt only INNER JOIN s on FIELD1 and FIELD2 , however this is an imperfect matching because I cannot differentiate between the first two groups of transactions.

SELECT 
    x.`TRNS_ID` AS x.`RA_TRNS_ID`, 
    y.`TRNS_ID` AS y.`CNTP_TRNS_ID`, 
    x.`FIELD1` as `RA_FIELD1`,
    y.`FIELD1` as `CNTP_FIELD1`,
    x.`FIELD2` as `RA_FIELD2`,
    y.`FIELD2` as `CNTP_FIELD2`
FROM transactions x
INNER JOIN transactions y
ON x.`CNTP_ID` = y.`RA_ID` AND x.`FIELD1` = y.`FIELD1` AND x.`FIELD2` = y.`FIELD2`
GROUP BY `RA_FIELD1`, `CNTP_FIELD1`, `RA_FIELD2`, `CNTP_FIELD2`
HAVING COUNT(*) = 2;

AND keyword should be in WHERE clauses instead of after ON keyword.

SELECT *
FROM transactions x
INNER JOIN transactions y
ON x.`CNTP_ID` = y.`RA_ID` 
WHERE (SELECT COLL FROM collaterals WHERE TRNS_ID = x.`ID`) = (SELECT COLL FROM collaterals WHERE TRNS_ID = y.`ID`)

Use multiple joins:

SELECT tx.*, ty.*
FROM transactions tx JOIN
     collaterals cx
     ON cx.trns_id = tx.id JOIN
     transactions ty
     ON tx.`CNTP_ID` = ty.`RA_ID` JOIN
     collaterals cy
     ON cy.trns_id = ty.id AND cy.COLL = cx.COLL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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