I am computing the frequency of values over a large table. since the result of the aggregate functions result in zero for some rows, I would like to simply skip such rows. I could have used NULLIF(..)
but I do not want to return a zero value. And aggregate functions aren’t allowed in the WHERE
clause. How do I refactor the my query to skip such rows?.
SELECT device_id, COUNT(*) / (MAX(signal_strength) - MIN(signal_strength)) as freq FROMm dcu WHERE MAX(signal_strength) - MIN(signal_strength) !=0 GROUP BY device_id ERROR: aggregate functions are not allowed in WHERE LINE 4: where max(signal_strength) - min(signal_strength) !=0
Advertisement
Answer
You must use HAVING
instead of WHERE
for aggregate computations. For example:
SELECT device_id, COUNT(*) / (MAX(signal_strength) - MIN(signal_strength)) as freq FROMm dcu GROUP BY device_id HAVING MAX(signal_strength) <> MIN(signal_strength)