SQL GROUP BY仅在子查询中

Der*_*ill 6 sql postgresql postgresql-9.5

我为不同品牌的不同日期的不同帐户的粉丝数量设置了一组数据点:

|brand|account|date|fans| 
|-----|-------|----|----|
|Ford |ford_uk|... |10  |
|Ford |ford_uk|... |11  |
|Ford |ford_us|... |20  | 
|Ford |ford_us|... |21  | 
|Jeep |jeep_uk|... |30  |
|Jeep |jeep_uk|... |31  |
|Jeep |jeep_us|... |40  |
|Jeep |jeep_us|... |41  |
Run Code Online (Sandbox Code Playgroud)

我试图按品牌返回粉丝总数,定义为每个品牌帐户的最大粉丝总和:

Ford: 32
Jeep: 72
Run Code Online (Sandbox Code Playgroud)

我尝试了这样的子查询:

(SELECT sum(account_fans)
  FROM
  (
    SELECT max(fans) AS account_fans
    GROUP BY account
  ) subquery_name
) AS total_fans
Run Code Online (Sandbox Code Playgroud)

问题是我得到:

错误:子查询使用外部查询中的未分组列帐户.

但我不想将外部查询分组.你能帮我吗?

Ste*_*ini 5

您是否尝试过以这种方式编写查询?

select  brand, sum(mx)
from    (
            select  brand, account, max(fans) mx
            from    account_fans
            group by brand, account
        ) t1
group by brand
Run Code Online (Sandbox Code Playgroud)