[英]Query to find combinations of accounts sql
我正在尋找如何形成查詢,我試圖發現訂購賬戶與相同的受益人賬戶交互 3 次或更多次。 正如我在下面描述的。
示例:賬戶A發送賬戶 1,2 和 3。賬戶B發送賬戶 1,2 和 3。賬戶C發送賬戶 1,2 和 3。
這是名為 TBL_ACCOUNTS 的表
訂購賬戶 | 受益人帳戶 |
---|---|
一個 | 1 |
乙 | 1 |
C | 1 |
一個 | 2 |
乙 | 2 |
C | 2 |
一個 | 3 |
乙 | 3 |
C | 3 |
H | 1 |
ķ | 23 |
Z | 329 |
W | 3 |
我想找到所有滿足此條件的帳戶,即訂購帳戶與相同的受益人帳戶交互 3 次或更多次。 你期望得到的結果是。
訂購賬戶 | 受益人帳戶 |
---|---|
一個 | 1 |
一個 | 2 |
一個 | 3 |
乙 | 1 |
乙 | 2 |
乙 | 3 |
C | 1 |
C | 2 |
C | 3 |
我希望你能指導我走哪條路,因為我有點迷路了。
您可以創建一個集合數據類型:
CREATE TYPE int_list IS TABLE OF INT;
然后你可以使用:
WITH accounts (ordering_account, beneficiary_account, accounts) AS (
SELECT t.*,
CAST(
COLLECT(beneficiary_account) OVER (PARTITION BY ordering_account)
AS int_list
)
FROM TBL_ACCOUNTS t
)
SELECT ordering_account,
beneficiary_account
FROM accounts a
WHERE EXISTS(
SELECT 1
FROM accounts x
WHERE a.ordering_account <> x.ordering_account
AND CARDINALITY(a.accounts MULTISET INTERSECT x.accounts) >= 3
-- Remove the next line if you want to return all accounts and not just the matched accounts
AND a.beneficiary_account = x.beneficiary_account
);
其中,對於樣本數據:
CREATE TABLE TBL_ACCOUNTS (ordering_account, beneficiary_account) AS
SELECT 'A', 1 FROM DUAL UNION ALL
SELECT 'B', 1 FROM DUAL UNION ALL
SELECT 'C', 1 FROM DUAL UNION ALL
SELECT 'A', 2 FROM DUAL UNION ALL
SELECT 'B', 2 FROM DUAL UNION ALL
SELECT 'C', 2 FROM DUAL UNION ALL
SELECT 'A', 3 FROM DUAL UNION ALL
SELECT 'B', 3 FROM DUAL UNION ALL
SELECT 'C', 3 FROM DUAL UNION ALL
SELECT 'C', 4 FROM DUAL UNION ALL
SELECT 'H', 1 FROM DUAL UNION ALL
SELECT 'K', 23 FROM DUAL UNION ALL
SELECT 'Z', 329 FROM DUAL UNION ALL
SELECT 'W', 3 FROM DUAL;
輸出:
ORDERING_ACCOUNT 受益人帳戶 一個 1 一個 3 一個 2 乙 1 乙 3 乙 2 C 1 C 2 C 3
如果你想在沒有集合的情況下這樣做:
SELECT ordering_account,
beneficiary_account
FROM TBL_ACCOUNTS a
WHERE EXISTS(
SELECT 1
FROM TBL_ACCOUNTS x
WHERE a.ordering_account <> x.ordering_account
AND a.beneficiary_account = x.beneficiary_account
AND EXISTS(
SELECT 1
FROM TBL_ACCOUNTS l
INNER JOIN TBL_ACCOUNTS r
ON (l.beneficiary_account = r.beneficiary_account)
WHERE l.ordering_account = a.ordering_account
AND r.ordering_account = x.ordering_account
HAVING COUNT(*) >= 3
)
);
或者:
SELECT ordering_account,
beneficiary_account
FROM TBL_ACCOUNTS a
WHERE EXISTS(
SELECT 1
FROM TBL_ACCOUNTS l
INNER JOIN TBL_ACCOUNTS r
ON ( l.beneficiary_account = r.beneficiary_account
AND l.ordering_account <> r.ordering_account )
WHERE l.ordering_account = a.ordering_account
GROUP BY r.ordering_account
HAVING COUNT(*) >= 3
AND COUNT(
CASE WHEN r.beneficiary_account = a.beneficiary_account THEN 1 END
) > 0
);
db<> 在這里擺弄
也許是這樣的:
select ordering_account, beneficiary
from TBL_ACCOUNTS
group by ordering_account, beneficiary
having count(*) >= 3
order by ordering_account, beneficiary
SELECT T.ordering_account,T.beneficiary_account
FROM TBL_ACCOUNTS T
JOIN
(
SELECT Z.ordering_account
FROM TBL_ACCOUNTS Z
GROUP BY Z.ordering_account
HAVING COUNT(*)>2
)X ON T.ordering_account=X.ordering_account
ORDER BY T.ordering_account,T.beneficiary_account
或者
SELECT X.ordering_account,X.beneficiary_account FROM
(
SELECT T.ordering_account,T.beneficiary_account,
COUNT(*)OVER(PARTITION BY T.ordering_account)XCOL
FROM TBL_ACCOUNTS T
)X WHERE X.XCOL=3
ORDER BY X.ordering_account,X.beneficiary_account
自加入受益人帳戶上的表。 因此,您獲得所有訂購賬戶對的頻率與它們共享 share3 受益賬戶的頻率一樣。 這意味着您可以按這些對分組並計數。
以下查詢列出了所有訂單賬戶的所有條目,其中存在另一個共享至少三個受益人賬戶的訂單賬戶。
with share3 as
(
select a1.ordering_account as acc1, a2.ordering_account as acc2
from tbl_accounts a1
join tbl_accounts a2 on a2.beneficiary_account = a1.beneficiary_account
and a2.ordering_account > a1.ordering_account
group by a1.ordering_account, a2.ordering_account
having count(*) >= 3
)
select *
from tbl_accounts
where exists
(
select null
from share3
where share3.acc1 = tbl_accounts.ordering_account
or share3.acc2 = tbl_accounts.ordering_account
)
order by ordering_account, beneficiary_account;
我不確定我是否遵循您的要求,但聽起來您只需要包含一個 ORDER BY 子句。
在您的查詢結束時只包括
ORDER BY 'ordering account', 'beneficiary account'
唯一可以改變這一點的是,如果您使用不喜歡單引號的不同類型的 SQL。 您可能需要使用 [],"" 或 ``。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.