我有一个查询:
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
和points
每个相关的location
。
我应该最终得到:
FRANCE |0|2|0|0|0|0|1 110.0
GERMANY |1|0|2|2|2|1|0 120.0
我相信我需要使用子查询和MAX(total)
,但我不能让它工作。在子查询中,我想选择points
,但我不想按它分组,这显然是不允许的。
我该怎么做呢?