无法在Group By - Oracle中添加子查询表达式

时间:2014-06-13 06:44:50

标签: sql oracle

我有如下查询,

select a,b,c, (select h from table 1 where field = a and field2 = b) as alias_column, d
from table 
group by a,b,c, (select h from table 1 where field = a and field2 = b) , d

我收到错误,

ORA-22818: subquery expressions not allowed here
22818. 00000 -  "subquery expressions not allowed here"
*Cause:    An attempt was made to use a subquery expression where these
           are not supported.
*Action:   Rewrite the statement without the subquery expression.
Error at Line: 84 Column: 2

我认为这是因为where clause中添加了子查询中的group by

请参阅子查询,(select * from table 1 where field = a and field2 = b)包含父表中的ab字段。

请帮忙。

2 个答案:

答案 0 :(得分:0)

如果子查询(select * from table 1 where field = a and field2 = b) 只返回一个值然后这应该工作:

select * from
(select a,b,c, (select * from table 1 where field = a and field2 = b) as alias_column, d
from table) 
group by a,b,c, alias_column , d

但是如果子查询(select * from table 1 where field = a and field2 = b) 如果没有返回一个值,那么您将得到以下错误:

ORA-01427: single-row subquery returns more than one row
01427. 00000 -  "single-row subquery returns more than one row"

答案 1 :(得分:-1)

为什么在没有涉及聚合函数的情况下进行分组?

使用DISTINCT代替,即

SELECT DISTINCT a,b,c,
       (select * from table 1
        where field = a and field2 = b) as alias_column, d
from table