In this section, we'll have a look at how groups can be filtered. There is a special keyword,
HAVING, reserved for that:
SUM(TotalSum) AS CustomerDailySum
GROUP BY CustomerId,
HAVING SUM(TotalSum) > 2000;
The new part here comes at the end. We've put the
HAVING keyword and then stated the condition on which to filter the groups. In this case, we only want to show those customers who, on individuals days, ordered goods for a total daily value exceeding 2000.
By the way, this is probably a good moment to note an important point: in T-SQL, the specific clauses must be always put in the right order. You can't, for example, put
HAVING must always follow
GROUP BY, not the other way around. Keep that in mind when you write your queries, especially longer ones.