我为不同品牌的不同日期的不同帐户的粉丝数量设置了一组数据点:
|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 |
我试图按品牌返回粉丝总数,定义为每个品牌帐户的最大粉丝总和:
Ford: 32 Jeep: 72
我尝试了这样的子查询:
(SELECT sum(account_fans) FROM ( SELECT max(fans) AS account_fans GROUP BY account ) subquery_name ) AS total_fans
问题是我得到:
ERROR: subquery uses ungrouped column account from outer query.
但我不想将外部查询分组.你能帮我吗?
解决方法
您是否尝试过以这种方式编写查询?
select brand,sum(mx) from ( select brand,account,max(fans) mx from account_fans group by brand,account ) t1 group by brand