Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
SQL Server
You can limit the groups that appear in a query by specifying a condition that applies to groups as a whole - a HAVING
clause. After the data has been grouped and aggregated, the conditions in the HAVING
clause are applied. Only the groups that meet the conditions appear in the query.
For example, you might want to see the average price of all books for each publisher in a titles
table, but only if the average price exceeds $10.00. In that case, you could specify a HAVING
clause with a condition such as AVG(price) > 10
.
Note
In some instances, you might want to exclude individual rows from groups before applying a condition to groups as a whole. For details, see Use HAVING and WHERE clauses in the same query (Visual Database Tools).
You can create complex conditions for a HAVING
clause by using AND
and OR
to link conditions. For details about using AND
and OR
in search conditions, see Specify multiple search conditions for one column (Visual Database Tools).
Specify a condition for a group
Specify the groups for your query. For details, see Group Rows in Query Results (Visual Database Tools).
If it's not already in the Criteria Pane (Visual Database Tools), add the column on which you want to base the condition. (Most often the condition involves a column that is already a group or summary column.) You can't use a column that isn't part of an aggregate function or of the
GROUP BY
clause.In the Filter column, specify the condition to apply to the group.
The Query and View Designer Tools (Visual Database Tools) automatically creates a
HAVING
clause in the statement in the SQL Pane (Visual Database Tools), such as in the following example:SELECT pub_id, AVG(price) FROM titles GROUP BY pub_id HAVING (AVG(price) > 10);
Repeat steps 2 and 3 for each additional condition you want to specify.