MySQL query to count results greater than 10 then group by

ben :

I have a MySQL table named water_test. Within this table I have an id, a province, and a sample_result.

| id | province | sample_result |
| 1  | prov_1   | 3             |
| 2  | prov_2   | 12            |
| 3  | prov_3   | 45            |
| 4  | prov_4   | 7             |
| 5  | prov_2   | 8             |
| 6  | prov_2   | 76            |
| 7  | prov_3   | 32            |
| 8  | prov_1   | 5             |
| 9  | prov_1   | 99            |
| 10 | prov_4   | 67            |
| 11 | prov_2   | 4             |

I would like to find all sample_results greater than 10 for each province.

The query result should look like this...

| province | count |
| prov_1   | 1     |
| prov_2   | 2     |
| prov_3   | 2     |
| prov_4   | 1     |

How would you express this in a MySQL query? I was thinking GROUP BY and HAVING COUNT and tried this...

SELECT sample_result
  FROM water_test
 GROUP BY province
HAVING COUNT(sample_result) > 10;

but for this query fails to run (error 1055).

GMB :

You can filter, then aggregate:

select province, count(*)
from mytable
where sample_result > 10
group by province

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=13838&siteId=1