Due to the nature of my query i have records with counts of 3 that would also fit the criteria of having count of 2 and so on. I was wondering is it possible to query 'having count more than x and less than 7' ? How could I write this. Here is my current code.
GROUP BY meetingID HAVING COUNT( caseID )<4
I'd like something like
GROUP BY meetingID HAVING COUNT( caseID )<4 AND >2
That way it would only count for exactly 3
The SQL HAVING Clause The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
This is because GROUP BY will only return unique results per group and the SELECT list can only consist aggregate functions or columns that are part of the GROUP BY clause. So depending on what you want to get, you can use different functions to get the optimal output.
The HAVING clause is applied nearly last, just before items are sent to the client, with no optimization. In other words, it is applied after the grouping operation has been performed (in contrast with WHERE , which is performed before any grouping operation).
The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.
GROUP BY meetingID HAVING COUNT(caseID) < 4 AND COUNT(caseID) > 2
There is no need to do two checks, why not just check for count = 3:
GROUP BY meetingID HAVING COUNT(caseID) = 3
If you want to use the multiple checks, then you can use:
GROUP BY meetingID HAVING COUNT(caseID) > 2 AND COUNT(caseID) < 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With