-2

我有一个 Oracle 查询,它的效果是

让 Count(field) > (返回一行的长 SQL 语句) 查询的双方单独工作,但在一起我得到一个“不是分组依据”表达式。

当用数字替换长 SQL 语句时,它可以工作,但如果只返回一行,我假设两者是等价的?

编辑 在玩了一些之后,我意识到: ... Table T ... Have Count(field) > (Long SQL statement with Table A Where A.field = T.field) 当我将 T.field 替换为任何T.field 的特定选项,但是当我专门引用 T.field 时,我得到相同的“不是按表达式分组”

4

2 回答 2

1

When Oracle parses your query it doesn't know if the query is going to return only one row or a bunch of rows. So simply append group by your_column to the end of your query.

For example this query returns one row:

select count(*) from user_objects;

But if I wanted to include sysdate along with that, I would have to do

select 
    sysdate the_date, 
    count(*) 
from
    user_objects 
group by 
    the_date;
于 2011-10-16T23:39:26.647 回答
0
SELECT ... 
FROM Table T ...
GROUP BY T.afield
HAVING Count(T.anotherfield) 
       > (Long SQL statement with Table A Where A.somefield = T.afield)

应该可以正常工作。


SELECT ... 
FROM Table T ...
GROUP BY T.anotherfield
HAVING Count(T.anotherfield) 
       > (Long SQL statement with Table A WHERE A.somefield = T.afield)

不应该工作。T.afield未包含在GROUP BY列表中的字段(如),不能在SELECT, HAVINGorORDER BY子句中引用。只能引用该字段的聚合函数 -WHERE A.somefield = MIN(T.afield)例如,您可以引用。

于 2011-10-17T00:30:02.980 回答