如何在中间父级上使用 'GROUP BY' 连接多个级别的父表?

How to join up multiple levels of parent tables with a 'GROUP BY' on an intermediate parent?

我的结构类似于:

country:  | state:         | city:        | citizen:
    id    |    id          |    id        |    id
    name  |    name        |    name      |    age
          |    country_id  |    state_id  |    city_id
          |                |              |    

我需要的结果是这样的:

country_name | state_name  | city_name | citizens
----------------------------------------------------
India        | MH          | Mumbai    | 45
USA          | California  | LA        | 234 
USA          | Washington  | Seattle   | 324

我正在尝试的查询是这样的:

SELECT 
    country.name AS "country_name",
    state.name AS "state_name",
    city.name AS "city_name",
    count(citizen.id) AS "citizens"
FROM citizen
LEFT JOIN city on citizen.city_id = city.id
LEFT JOIN state on city.state_id = state.id
LEFT JOIN country on state.country_id = country.id
WHERE 
     citizen.age > 50
GROUP BY city_name;

Postgres 给我的错误是这样的:

ERROR:  column "state.name" must appear in the GROUP BY clause
        or be used in an aggregate function
LINE 3: state.name AS "state_name",

我不确定如何实现我的需求。

我会单独做这个joins

select cn.name as country_name, st.name as state_name, 
       ct.name as city_name, c.citizens
from (
     select city_id, count(*) as citizens
     from citizen
     where age > 50
     group by city_id
) c left join city ct on ct.id  = c.city_id
left join state st on st.id = ct.state_id
left join country cn on cn.id = st.country_id;