在子查询,标准或Oracle功能中混合聚合值和非聚合值?

时间:2013-11-05 21:29:44

标签: sql oracle oracle10g standards

当我在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 

1 个答案:

答案 0 :(得分: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;

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