Suppose there are two tables
* ----------------- 1
site_visits sites
site_id | visits site_id
1 | 15 1
1 | 10 2
2 | 20 3
2 | 45
3 | 55
The purpose is to count how many sites, have more than 50 visits. The DBMS is MySQL with InnoDB. To be noted, that there must be a join, because there are where clauses on columns from both tables (in the bigger picture).
I've managed to write this with a sub-query in FROM; from the above data, it should yield the value 2, since there are 2 such sites, with id 2 and 3 having sums 65 and 55, while for id 1 the sum is only 25.
select count(*)
from (
select sum(visit.visits) as visits_sum
from sites site
join site_visits visit
on site.site_id = visit.site_id
group by site.site_id) as sub_sum
where visits_sum < 50
However I'd like this written without a sub-query in FROM in order to be able to use it with ORM; what I've managed thus far is:
select count(site.site_id)
from sites site
join site_visits visit
on site.site_id = visit.site_id
group by site.site_id
having sum(visit.visits) < 50
Without the group by, it sums the whole joined table and yields 3. With it, it returns as many entries as the total count, in this case 2. And the values for these entries are 2 and 1 (since there are 2 entries with id 2 and 1 with id 3 in the joined table). Something like count(count(...))
would probably do it, but that's not allowed.