ORA-01427:单行子查询返回多个行select语句

时间:2014-05-29 06:56:16

标签: sql oracle

我有两个结果集。我打算获取第一个结果集中的记录,而不是第二个结果集中的记录: 我的第一个结果集:

select * from (select k.foracid,k.acct_name, k.sol_id BRANCH_CODE, s.SOL_DESC BRANCH_NAME,k.ACCT_OPN_DATE,k.schm_code 
from tbaadm.gam k, tbaadm.sol s
where acct_cls_flg = 'N' and acct_ownership<>'O'
and s.sol_id = k.sol_id
and k.schm_type in('CAA', 'SBA', 'ODA', 'TDA'));

这里Foracid是独一无二的:

我的第二个结果:

select acctid from (select r.signid,
(case when r.acctid is not null then r.acctid else (select g.foracid from tbaadm.gam g where g.cif_id = r.custid) end)acctid,
(case when r.custid is not null then r.custid else (select i.cif_id from tbaadm.gam i where i.foracid = r.acctid) end)custid
from
(select cast(e.signid as varchar2(12))signid, cast(e.acctid as varchar2(16))acctid, 
cast(f.custid as varchar2(12))custid from svsuser.signotherinfo e, svsuser.signcustinfo f
where e.signid = f.signid)r);

这里我的acctid并不是唯一的,但它与第一​​个select语句中的foracid具有相同的值。现在我这样做:

select * from (select k.foracid,k.acct_name, k.sol_id BRANCH_CODE, s.SOL_DESC BRANCH_NAME,k.ACCT_OPN_DATE,k.schm_code 
    from tbaadm.gam k, tbaadm.sol s
    where acct_cls_flg = 'N' and acct_ownership<>'O'
    and s.sol_id = k.sol_id
    and k.schm_type in('CAA', 'SBA', 'ODA', 'TDA'))m
where m.foracid not in(
select acctid from (select r.signid,
    (case when r.acctid is not null then r.acctid else (select g.foracid from tbaadm.gam g where g.cif_id = r.custid) end)acctid,
    (case when r.custid is not null then r.custid else (select i.cif_id from tbaadm.gam i where i.foracid = r.acctid) end)custid
    from
    (select cast(e.signid as varchar2(12))signid, cast(e.acctid as varchar2(16))acctid, 
    cast(f.custid as varchar2(12))custid from svsuser.signotherinfo e, svsuser.signcustinfo f
    where e.signid = f.signid)r));

我得到了:

ORA-01427: single-row subquery returns more than one row

我应该做什么?

1 个答案:

答案 0 :(得分:2)

查询中出现此错误的唯一地方是这两个SELECT语句:

select g.foracid from tbaadm.gam g where g.cif_id = r.custid
...
select i.cif_id from tbaadm.gam i where i.foracid = r.acctid

你确定,对于每个cif_id,tbaadm.gam中只有一行,所以对于每个foracid?请尝试使用以下两个查询进行检查:

SELECT CIF_ID
FROM tbaadm.gam
HAVING COUNT(1) > 1
GROUP BY CIF_ID;

SELECT FORACID
FROM tbaadm.gam
HAVING COUNT(1) > 1
GROUP BY FORACID;

如果有多行,也可以尝试添加DISTINCT或AGGREGATE函数。例如:

select * from (select k.foracid,k.acct_name, k.sol_id BRANCH_CODE, s.SOL_DESC BRANCH_NAME,k.ACCT_OPN_DATE,k.schm_code 
    from tbaadm.gam k, tbaadm.sol s
    where acct_cls_flg = 'N' and acct_ownership<>'O'
    and s.sol_id = k.sol_id
    and k.schm_type in('CAA', 'SBA', 'ODA', 'TDA'))m
where m.foracid not in(
select acctid from (select r.signid,
      (case when r.acctid is not null then r.acctid else (select MIN(g.foracid) from tbaadm.gam g where g.cif_id = r.custid) end)acctid,
    (case when r.custid is not null then r.custid else (select MIN(i.cif_id) from tbaadm.gam i where i.foracid = r.acctid) end)custid
    from
    (select cast(e.signid as varchar2(12))signid, cast(e.acctid as varchar2(16))acctid, 
    cast(f.custid as varchar2(12))custid from svsuser.signotherinfo e, svsuser.signcustinfo f
    where e.signid = f.signid)r));

希望,这有帮助。