I am currently trying to get the following statement to work:
SELECT t1.id, t1.row2
FROM table1 t1
JOIN table2 t2 ON t1.rowx = t2.rowx
JOIN table3 t3 ON t2.rowy = t3.rowy
GROUP BY t1.row2
HAVING COUNT(t2.rowx) < t3.maximum_size
The statement works and provides an output, however a lot of the t3.maximum_size fields are NULL which then prevent the statement working as desired.
Is there a way to have a COUNT(*) < another_number including NULL values?
Example:
10 < 20 // true
20 < 20 // false
18 < null // true (as null represents no maximum size in my query)
(I did check other SO questions which there are lots relating to the issue, but I haven’t seen one helping with this specific task – if I have missed one please point me in the right direction.)
Change the having clause to:
In retrospect, a better SQL answer is:
The original syntax works in mysql but not in most databases. Also, I consider it to be bad SQL. The use of “t3.maximum_size” is really equivalent to “any value for t3.Maximum_Size” when referred to in an aggregation context. It is much better to be explicit.