oraclesql-Join和have
我正在尝试获取在contract表中多次出现的帐户,而不管它在c_map表中加入了多少次oraclesql-Join和have,sql,Sql,我正在尝试获取在contract表中多次出现的帐户,而不管它在c_map表中加入了多少次 CREATE TABLE cntrct ( cntrct_id VARCHAR(10), account_id varchar(10) ); CREATE TABLE c_map ( cntrct_id VARCHAR(10) ); INSERT INTO cntrct VALUES (1,'a'); insert into cntrct values (2,'b'); insert int
CREATE TABLE cntrct (
cntrct_id VARCHAR(10),
account_id varchar(10)
);
CREATE TABLE c_map (
cntrct_id VARCHAR(10)
);
INSERT INTO cntrct VALUES (1,'a');
insert into cntrct values (2,'b');
insert into cntrct values (3,'c');
insert into cntrct values (4,'b');
INSERT INTO c_map VALUES (1);
INSERT INTO c_map VALUES (1);
INSERT INTO c_map VALUES (1);
insert into c_map values (2);
insert into c_map values (2);
insert into c_map values (2);
insert into c_map values (3);
insert into c_map values (3);
insert into c_map values (3);
commit;
select ct.account_id
from cntrct ct, c_map cm
where ct.cntrct_id = cm.cntrct_id
group by ct.account_id
having count(ct.account_id)> 1
小提琴:
我期望输出为:
b
但是相反,我得到了所有这些。在运行having count()>1时,如何限制它,使其不考虑c_映射表
谢谢
获取在合同表中多次出现的帐户,无论其在c_映射表中加入的次数如何
CREATE TABLE cntrct (
cntrct_id VARCHAR(10),
account_id varchar(10)
);
CREATE TABLE c_map (
cntrct_id VARCHAR(10)
);
INSERT INTO cntrct VALUES (1,'a');
insert into cntrct values (2,'b');
insert into cntrct values (3,'c');
insert into cntrct values (4,'b');
INSERT INTO c_map VALUES (1);
INSERT INTO c_map VALUES (1);
INSERT INTO c_map VALUES (1);
insert into c_map values (2);
insert into c_map values (2);
insert into c_map values (2);
insert into c_map values (3);
insert into c_map values (3);
insert into c_map values (3);
commit;
select ct.account_id
from cntrct ct, c_map cm
where ct.cntrct_id = cm.cntrct_id
group by ct.account_id
having count(ct.account_id)> 1
为什么要加入呢?您需要的信息在合同表中:
select account_id
from cntrct
group by account_id
having count(*) > 1
也许您希望根据映射表中存在的合同id筛选数据集。如果是这样,我建议存在:
select account_id
from cntrct c
where exists (select 1 from c_map m where m.cntrct_id = c.cntrct_id)
group by account_id
having count(*) > 1
获取在合同表中多次出现的帐户,无论其在c_映射表中加入的次数如何
CREATE TABLE cntrct (
cntrct_id VARCHAR(10),
account_id varchar(10)
);
CREATE TABLE c_map (
cntrct_id VARCHAR(10)
);
INSERT INTO cntrct VALUES (1,'a');
insert into cntrct values (2,'b');
insert into cntrct values (3,'c');
insert into cntrct values (4,'b');
INSERT INTO c_map VALUES (1);
INSERT INTO c_map VALUES (1);
INSERT INTO c_map VALUES (1);
insert into c_map values (2);
insert into c_map values (2);
insert into c_map values (2);
insert into c_map values (3);
insert into c_map values (3);
insert into c_map values (3);
commit;
select ct.account_id
from cntrct ct, c_map cm
where ct.cntrct_id = cm.cntrct_id
group by ct.account_id
having count(ct.account_id)> 1
为什么要加入呢?您需要的信息在合同表中:
select account_id
from cntrct
group by account_id
having count(*) > 1
也许您希望根据映射表中存在的合同id筛选数据集。如果是这样,我建议存在:
select account_id
from cntrct c
where exists (select 1 from c_map m where m.cntrct_id = c.cntrct_id)
group by account_id
having count(*) > 1
因为它被简化了。我需要考虑c_映射表中的一个条件,这里没有表示。不过概念是一样的。谢谢你,这很有效。因为它简化了。我需要考虑c_映射表中的一个条件,这里没有表示。不过概念是一样的,谢谢你。