CREATE TABLE one_many
(name VARCHAR2(20),
acc_num VARCHAR2(20))
INSERT ALL
INTO one_many VALUES ('abc','a-110')
INTO one_many VALUES ('def','b-256')
INTO one_many VALUES ('xyz','v-586')
INTO one_many VALUES ('uij','o-451')
INTO one_many VALUES ('pqr','')
INTO one_many VALUES ('abc','a-120')
INTO one_many VALUES ('xyz','v-588')
SELECT * FROM dual;
如何找到一个多个案例,即选择那些具有多个acc_num的名称
Desired o/p:
NAME ACC_NUM
abc a-110
abc a-120
xyz v-586
xyz v-588
答案 0 :(得分:4)
在这种情况下,您需要使用GROUP BY
和HAVING
。
SELECT name, COUNT(*) totalCount
FROM one_many
GROUP BY name
HAVING COUNT(*) > 1
或
也许这就是你的意思。
SELECT a.*
FROM one_many a
INNER JOIN
(
SELECT name, COUNT(*) totalCount
FROM one_many
GROUP BY name
HAVING COUNT(*) > 1
) b ON a.name = b.name
答案 1 :(得分:1)
此查询似乎是为分析函数设计的:
select name, acc_num
from (select om.*,
count(*) over (partition by name) as NumOnAccount
from one_many om
) t
where NumOnAccount > 1
答案 2 :(得分:0)
SELECT * FROM one_many
WHERE acc_num IN
(SELECT acc_num FROM
one_many
GROUP BY name ,acc_num
HAVING COUNT(*)>1
);