简体   繁体   English

查询以查找帐户 sql 的组合

[英]Query to find combinations of accounts sql

I am looking for how to form a query, where I seek to find that the ordering accounts are interacting with the same beneficiary accounts 3 or more times.我正在寻找如何形成查询,我试图发现订购账户与相同的受益人账户交互 3 次或更多次。 As I describe below.正如我在下面描述的。
Examples: Account A sends account 1,2,and 3. Account B sends account 1,2 and 3. Account C sends account 1,2 and 3.示例:账户A发送账户 1,2 和 3。账户B发送账户 1,2 和 3。账户C发送账户 1,2 和 3。

This is the table called TBL_ACCOUNTS这是名为 TBL_ACCOUNTS 的表

ordering account订购账户 beneficiary account受益人帐户
A一个 1 1
B 1 1
C C 1 1
A一个 2 2
B 2 2
C C 2 2
A一个 3 3
B 3 3
C C 3 3
H H 1 1
K ķ 23 23
Z Z 329 329
W W 3 3

I want to find all those accounts that meet this condition, that the ordering accounts are interacting with the same beneficiary accounts 3 or more times.我想找到所有满足此条件的帐户,即订购帐户与相同的受益人帐户交互 3 次或更多次。 The result you would expect to get is.你期望得到的结果是。

ordering account订购账户 beneficiary account受益人帐户
A一个 1 1
A一个 2 2
A一个 3 3
B 1 1
B 2 2
B 3 3
C C 1 1
C C 2 2
C C 3 3

I hope you can guide me which way to go, because I'm a bit lost.我希望你能指导我走哪条路,因为我有点迷路了。

You can create a collection data type:您可以创建一个集合数据类型:

CREATE TYPE int_list IS TABLE OF INT;

and then you can use:然后你可以使用:

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
       );

Which, for the sample data:其中,对于样本数据:

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;

Outputs:输出:

ORDERING_ACCOUNT ORDERING_ACCOUNT BENEFICIARY_ACCOUNT受益人帐户
A一个 1 1
A一个 3 3
A一个 2 2
B 1 1
B 3 3
B 2 2
C C 1 1
C C 2 2
C C 3 3

If you want to do it without a collection then:如果你想在没有集合的情况下这样做:

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
                )
       );

or:或者:

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<>fiddle here db<> 在这里摆弄

Maybe something like this:也许是这样的:

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

or或者

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

Self-join the table on the beneficiary account.自加入受益人帐户上的表。 Thus you get all ordering account pairs as often as they share the share3 beneficiary accounts.因此,您获得所有订购账户对的频率与它们共享 share3 受益账户的频率一样。 This means you can group by these pairs then and count.这意味着您可以按这些对分组并计数。

The following query lists all entries of all ordering accounts for which exists another ordering account sharing at least three beneficiary accounts.以下查询列出了所有订单账户的所有条目,其中存在另一个共享至少三个受益人账户的订单账户。

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;

I'm not sure I follow what you're asking, but it sounds like you simply need to include an ORDER BY clause.我不确定我是否遵循您的要求,但听起来您只需要包含一个 ORDER BY 子句。

At the end of your query just include在您的查询结束时只包括

ORDER BY 'ordering account', 'beneficiary account'

The only thing that could change this is if you use different kinds of SQL that don't like single quotes.唯一可以改变这一点的是,如果您使用不喜欢单引号的不同类型的 SQL。 You may need to use [],"", or ``.您可能需要使用 [],"" 或 ``。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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