-2

Oracleクエリがあります。

Count(field)>(1行を返す長いSQLステートメント)クエリの両側は単独で機能しますが、一緒に「groupbyではない」式を取得します。

長いSQLステートメントを数値に置き換えると機能しますが、1行だけが返される場合、2つは同等であると思いましたか?

編集 いくつか遊んだ後、私は気づきました:...テーブルT ... Count(field)>(テーブルAの長いSQLステートメントここでA.field = T.field)T.fieldを次のいずれかに置き換えると機能しますT.fieldの特定のオプションですが、T.fieldを具体的に参照すると、同じ「groupby式ではありません」が表示されます。

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)

should work ok.


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

should not work. A field (like T.afield) that is not included in the GROUP BY list, cannot be referenced in SELECT, HAVING or ORDER BY clauses. Only aggregate functions of that field can be referenced - you could have WHERE A.somefield = MIN(T.afield) for example.

于 2011-10-17T00:30:02.980 に答える