GROUP BY - HAVING Restrictions
HAVING statement is used in order to include and extract records' groups out of query results; that's why the used it selection condition is applied not to the separate records but to the whole group. It means that following elements can be used in the selection condition:
- aggregative function (returns one value for all the records included to the group);
- group field (has the same value in all the group records by declaration);
- the expression which include all above mentioned elements.
In practice the selection condition of HAVING statement should include at the minimum one aggregative function. If it isn't so you can move the selection condition to the WHERE statement. To define the location of selection condition in WHERE or HAVING statement, - you should remind the way of these statements application:
- WHERE statement is applied to the separate records, so expressions contained in it should be computed for the separate records;
- HAVING statement is applied to the records groups, so expressions contained in it should be computed for records groups.