【发布时间】:2012-06-21 15:27:16
【问题描述】:
我有一个问题:
select substr(name,7,50) as location, points,sum(if (p1=r1,10,-10))as total from
dq.data
group by points,location order by location,total desc
产生此数据的设备:
FRANCE |0|2|0|0|0|0|1 110.0
FRANCE |0|2|1|0|1|2|1 100.0
FRANCE |0|2|0|0|0|1|1 100.0
FRANCE |0|2|1|0|0|1|1 100.0
FRANCE |0|2|0|1|1|2|1 100.0
FRANCE |0|2|0|0|1|1|1 100.0
GERMANY |1|0|2|2|2|1|0 120.0
GERMANY |1|0|2|2|2|0|0 110.0
GERMANY |1|0|2|2|2|2|0 110.0
GERMANY |1|0|2|2|2|0|2 110.0
GERMANY |1|0|2|2|2|1|1 110.0
我想达到最高的total 和每个location 的相关points。
我最终应该得到:
FRANCE |0|2|0|0|0|0|1 110.0
GERMANY |1|0|2|2|2|1|0 120.0
我相信我需要使用子查询和MAX(total),但我无法让它工作。
在子查询中,我想选择points,但我不想按它进行分组,这显然是不允许的。
我该怎么做?
【问题讨论】:
标签: sql google-bigquery