2012-06-21 38 views
1

分組最高值我有一個查詢:從與一個由查詢

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,但我不想通過它進行分組,這顯然是不允許的。

我該如何去做這件事?

回答

3

你的直覺是正確的。您可以通過計算最大總,然後加入這一回原來的數據做:

select t.* 
from (select substr(name,7,50) as location, points,sum(if (p1=r1,10,-10))as total 
     from dq.data 
     group by points,location 
    ) t join 
    (select location, max(total) as maxtotal 
     from (select substr(name,7,50) as location, points,sum(if (p1=r1,10,-10))as total 
      from dq.data 
      group by points,location 
      ) t 
     group by location 
    ) tsum 
    on t.location = tsum.location and t.total = tsum.maxtotal 

注意,這個版本將返回重複,如果有在頂部的聯繫。

我不是很熟悉google-biggquery。如果支持「與」語句,那麼你就可以簡化查詢,這樣做:

with t as (select substr(name,7,50) as location, points,sum(if (p1=r1,10,-10))as total 
      from dq.data 
      group by points,location 
     ) 
select t.* 
from t join 
    (select location, max(total) as maxtotal 
     from t 
     group by location 
    ) tsum 
    on t.location = tsum.location and t.total = tsum.maxtotal 

如果它支持Windows的功能(如ROW_NUMBER()),那麼你就可以消除明確乾脆加入。

+0

謝謝戈登,我會放棄這一點。它不支持row_number(),它也不允許你選擇*(或t。*我認爲)。我假設我可以硬編碼字段的名稱? –

+0

重視領域是正確的選擇。我只是在答案中使用「*」,因爲它的輸入速度更快。不過,一般來說,你想要明確字段名稱。 –

+0

這很好 - 我試了一下,報告回來,如果一切正常,我會在那個時候接受。再次感謝 –

0

我最近有一個類似的問題,解決它與此類似:

如果
SELECT substr(name,7,50) as location, points,sum(if (p1=r1,10,-10))as total 
FROM ( 
    SELECT * FROM dq.data ORDER BY location,sum(if (p1=r1,10,-10)) desc 
) tmp 
GROUP BY points,location; 

不知道它會工作作爲我的數據庫是MySQL的,但它是一個不錯的直觀的解決方案。按照您希望彙總行消失的方式對子查詢進行排序。

+0

標準SQL不支持子查詢中的「order by」,所以這在mst數據庫上不起作用。 –

+0

我得到了:BAD_QUERY(位置出現在ORDER BY中,但它不是SELECT中的命名列),然後在明確聲明它時:BAD_QUERY(表達式SUM(IF([p1] = [r1],10, - 10)), DESC)無效) –

+0

SELECT substr(name,7,50)as location,points,sum(if(p1 = r1,10,-10))as total FROM( SELECT substr(name,7, 50)作爲位置,點數,總和(如果(p1 = r1,10,-10))作爲總數FROM dq.data ORDER BY location,total desc )tmp GROUP BY points,location; – chim