Sql 识别单行子查询错误
我有一个引发错误的Oracle SQL查询:Sql 识别单行子查询错误,sql,oracle,subquery,Sql,Oracle,Subquery,我有一个引发错误的Oracle SQL查询: ORA-01427: single-row subquery returns more than one row 01427. 00000 - "single-row subquery returns more than one row" *Cause: *Action: 如何避免此错误?如何确定此错误的原因/来源 下面是SQL查询: select (select CID from SPL A
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
*Cause:
*Action:
如何避免此错误?如何确定此错误的原因/来源
下面是SQL查询:
select (select CID
from SPL A
where A.prodid = appl.prod_id
and A.STATUS = 'SET'
and A.DT = (select min(DT) from SPL B where A.prodid = B.prodid))
as CIDORIG
from prod_master appl
where prod_status = 'OFF';
但是,我无法在SQL语句中找到这样的子查询。MIN()只返回一个结果。我还尝试用关键字IN替换“=”符号,但没有成功。此查询适用于其他产品状态值
在抛出错误之前,它从我们预期的15648行中获取13700行。我预计会有15648行,因为SQLDeveloper配置为一次只返回50行。当我选择“计算行数”选项时,它给出的数字是15648
SPL和prod_master都是视图。可能返回多行的子查询是:
(select CID
from SPL A
where A.prodid = appl.prod_id
and A.STATUS = 'SET'
and A.DT = (select min(DT) from SPL B where A.prodid = B.prodid)
) as CIDORIG
若要解决此问题,请尝试选择
min(CID)
或max(CID)
仅当您确定子查询将返回1行或0行时才使用标量子查询,否则将导致此错误
您可以编写查询而不使用标量子查询,如下所示
select a.CID
from prod_master appl
join SPL A
on ( A.prodid = appl.prod_id)
where appl.prod_status = 'OFF'
and A.STATUS = 'SET'
and A.DT = (select min(b.DT)
from SPL B
where A.prodid = B.prodid);