You make a two mistakes
- First: you have try to calulate COUNT() in only one (I mean, the second) table. This doesn't will work because theCOUNT(), like an any aggregate function, calculates only for the whole set of rows, not just for any part of the set (not only just for the one or an other joined table).
In your first query, you may replace secteur. * only by asterisk, like a Region.region_id, count(*) AS count
, and do not forget add Region.region_id on the GROUP BY
step.
- Second: You has define not only aggregate function in the query, but and other fields:
select Region.*
, but you don't define them in GROUP BY
step. You need to add to GROUP BY
statement all columns, which you has define in the SELECT
step but not apply an aggregate functions to them.
- Append: not,
GROUP BY Region.*
doesn't will work, you should to define a columns in the GROUP BY
step by their actual names.
So, correct form of this will looks like a
SELECT
Region.col1
,Region.col2,
, count(*) count
from Region
left join
secteur on secteur.region_id = Region.id
GROUP BY Region.col1, Region.col2
Or, if you don't want to type each name of column, use window queries
SELECT
Region.*,
, count( * ) OVER (PARTITION BY region_id) AS count
from Region
left join
secteur on secteur.region_id = Region.id