需要Oracle查询与多个fund_idMap的单个account_id

pgccezyw  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(123)

我需要处理一个像下面这样的数据集:

account_id  Fund_id
    
ASC23454    765432
ASC67654    988776
ASC23454    899098
ASC23454    765787
ASC90878    899877
ASC90878    788978
ASC98765    889909
ASC09876    908990

字符串
因此,我需要在Oracle中编写一个查询,它将只显示具有多个不同fund_idaccount_id
预期输出:

account_id  Fund_id
    
ASC23454    765432
ASC23454    899098
ASC23454    765787
ASC90878    899877
ASC90878    788978


任何人都可以请帮助我获得上述输出查询。

cgh8pdjw

cgh8pdjw1#

使用解析函数COUNT来计算每个account_idfund_idDISTINCT数量:

SELECT account_id,
       fund_id
FROM   (
  SELECT account_id,
         fund_id,
         COUNT(DISTINCT fund_id) OVER (PARTITION BY account_id) AS num_funds
  FROM   table_name
)
WHERE  num_funds > 1;

字符串
其中,对于样本数据:

CREATE TABLE table_name (account_id, Fund_id) AS
SELECT 'ASC23454', 765432 FROM DUAL UNION ALL
SELECT 'ASC67654', 988776 FROM DUAL UNION ALL
SELECT 'ASC23454', 899098 FROM DUAL UNION ALL
SELECT 'ASC23454', 765787 FROM DUAL UNION ALL
SELECT 'ASC90878', 899877 FROM DUAL UNION ALL
SELECT 'ASC90878', 788978 FROM DUAL UNION ALL
SELECT 'ASC98765', 889909 FROM DUAL UNION ALL
SELECT 'ASC09876', 908990 FROM DUAL;


产出:
| 联系我们|基金_ID|
| --|--|
| ASC23454| 765432 |
| ASC23454| 765787 |
| ASC23454| 899098 |
| ASC90878| 788978 |
| ASC90878| 899877 |
fiddle

相关问题