0

当我在 Oracle 10g 中运行它时,我收到错误“ORA-01427:单行子查询返回多于一行”,正如我所料:

WITH a as (select  1 somenumber  from dual),
     b as (           select 'foo' foo from dual 
            union all select 'goo'     from dual)
SELECT (select 0.5 from b) 
FROM a 

但是,当我运行以下语句时,我没有收到错误,而是返回“2.5”。

WITH a as (select  1 somenumber  from dual),
     b as (           select 'foo' foo from dual 
            union all select 'goo'     from dual)
SELECT (select sum(a.somenumber)  + 0.5 from b) 
FROM a 

这个标准是对子查询求和然后添加 0.5 就好像它是在运行子查询后完成的一样?或者这是一些 Oracle 特性/错误?

编辑:对于那些好奇的人,实际代码看起来更像这样:

WITH a as (select  1 somenumber  from dual),
     b as (           select 'foo' foo from dual 
            union all select 'goo'     from dual)
SELECT (select sum(a.somenumber)  + customfunction(a.somenumber) from b) 
FROM a 
4

1 回答 1

2

答案是:这是标准的。您的查询是:

SELECT (select sum(a.somenumber)  + 0.5
        from b
       )  as sum_from_b
FROM a 

对于 中的每一行b,它加起来a.somenumber然后加 0.5。您可以将常数添加到计算表达式中。这是标准 SQL。

请注意,您真正在做的是:

SELECT a.somenumber * (select count(*) from b) + 0.5
FROM a;

在这种情况下,没有理由有一个相关的子查询。

于 2013-11-05T21:38:59.597 回答