[英]Find missing pairs (Oracle SQL)
Lets say that I have a table called customer_accounts
that looks like this:假设我有一个名为customer_accounts
的表,如下所示:
Table: CUSTOMER_ACCOUNTS表:CUSTOMER_ACCOUNTS
+------------------+--------------+
| CUSTOMER_ID (PK) | ACCOUNT_ID |
+------------------+--------------+
| 1 | 5 |
| 1 | 6 |
| 1 | 8 |
+------------------+--------------+
And I have another table called distinct_account_pairs
that looks like this:我还有另一个名为distinct_account_pairs
的表,如下所示:
Table: DISTINCT_ACCOUNT_PAIRS (before)表:DISTINCT_ACCOUNT_PAIRS(之前)
+------------------+---------------+---------------+
| CUSTOMER_ID (PK) | ACCOUNT_ID1 | ACCOUNT_ID2 |
+------------------+---------------+---------------+
| 1 | 6 | 5 |
+------------------+---------------+---------------+
How do I populate the distinct_account_pairs table with the missing pairs for each CUSTOMER_ID?如何使用每个 CUSTOMER_ID 的缺失对填充 distinct_account_pairs 表? In this example 5 and 8, 6 and 8 are missing for PK CUSTOMER_ID 1 in distinct_account_pairs, so after it should look something like this.在此示例中,distinct_account_pairs 中的 PK CUSTOMER_ID 1 缺少 5 和 8、6 和 8,因此它应该看起来像这样。 Note that distinct_account_pairs could have account_id1 < account_id2 or vice versa.请注意 distinct_account_pairs 可能有 account_id1 < account_id2 ,反之亦然。 The pairs just need to be distinct for each CUSTOMER_ID.对于每个 CUSTOMER_ID,这些对只需要不同。
Table: DISTINCT_ACCOUNT_PAIRS (after)表:DISTINCT_ACCOUNT_PAIRS(之后)
+------------------+---------------+---------------+
| CUSTOMER_ID (PK) | ACCOUNT_ID1 | ACCOUNT_ID2 |
+------------------+---------------+---------------+
| 1 | 6 | 5 |
| 1 | 5 | 8 |
| 1 | 6 | 8 |
+------------------+---------------+---------------+
You can do it with self join and not exists:您可以通过自我加入来做到这一点并且不存在:
create table CUSTOMER_ACCOUNTS ( CUSTOMER_ID int, ACCOUNT_ID int);
insert into CUSTOMER_ACCOUNTS values( 1 , 5 );
insert into CUSTOMER_ACCOUNTS values( 1 , 6 );
insert into CUSTOMER_ACCOUNTS values( 1 , 8 );
create table DISTINCT_ACCOUNT_PAIRS ( CUSTOMER_ID int, ACCOUNT_ID1 int, ACCOUNT_ID2 int);
insert into DISTINCT_ACCOUNT_PAIRS values( 1 , 6 , 5 );
Insert Query:插入查询:
insert into DISTINCT_ACCOUNT_PAIRS
select a.CUSTOMER_ID,a.ACCOUNT_ID,b.ACCOUNT_ID from CUSTOMER_ACCOUNTS a inner join CUSTOMER_ACCOUNTS b
on a.CUSTOMER_ID = b.CUSTOMER_ID and a.ACCOUNT_ID < b.ACCOUNT_ID
and not exists
(select 1 from DISTINCT_ACCOUNT_PAIRS d where d.CUSTOMER_ID=a.CUSTOMER_ID
and ((d.ACCOUNT_ID1=a.ACCOUNT_ID and d.ACCOUNT_ID2=b.ACCOUNT_ID)
or (d.ACCOUNT_ID1=b.ACCOUNT_ID and d.ACCOUNT_ID2=a.ACCOUNT_ID))
);
Query: to select from DISTINCT_ACCOUNT_PAIRS查询:从 DISTINCT_ACCOUNT_PAIRS 到 select
SELECT * FROM DISTINCT_ACCOUNT_PAIRS;
Output: Output:
CUSTOMER_ID客户ID | ACCOUNT_ID1 ACCOUNT_ID1 | ACCOUNT_ID2 ACCOUNT_ID2 |
---|---|---|
1 1 | 6 6 | 5 5 |
1 1 | 6 6 | 8 8 |
1 1 | 5 5 | 8 8 |
You would generate all the pairs and then compare them:您将生成所有对,然后比较它们:
select ca1.customer_id, ca1.account_id, ca2.account_id
from customer_accounts ca1 join
customer_accounts ca2
on ca1.customer_id = ca2.customer_id and
ca1.account_id < ca2.account_id left join
distinct_account_pairs dap
on dap.customer_id = ca1.customer_id and
dap.account_id1 in (ca1.account_id, ca2.account_id) and
dap.account_id2 in (ca1.account_id, ca2.account_id)
where dap.customer_id is null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.