2017-10-10 238 views
0

我正在做運動分析(沙灘排球),並得到了一張表moves。舉例來說,移動是服務王牌,阻止錯誤或攻擊獲勝者(由標籤標識)。一個舉措可以提高a隊,b隊的得分或者保持原狀。PostgreSQL計算總和和使用結果返回贏家

部分樣本moves數據。

set, tag, points_team_a, points_team_b 
---,----,--------------,-------------- 
    1, 520,    1,    0 
    1, 510,    0,    0 
    1, 300,    0,    1 
    1, 410,    0,    0 
    1, 620,    0,    0 

我有一個查詢返回整體結果。

select 
    moves.set, 
    sum(moves.points_team_a) as team_a, 
    sum(moves.points_team_b) as team_b 
from moves 
where match_uuid = '26d41fc6-13d3-4af7-a5a3-3ec21bf06f03' 
group by set 

這表明team_a拿下首盤21:12和第二組21:18。

set, team_a, team_b 
    1,  21,  12 
    2,  21,  18 

是否可以將總體匹配結果(在本例中爲team_a爲2:0)添加到查詢中?

+1

編輯您的問題,並提供樣本數據和預期結果。 –

回答

1

這是你想要的嗎?

select m.*, 
     sum((team_a > team_b)::int) over (order by set) as running_a_wins, 
     sum((team_b > team_a)::int) over (order by set) as running_b_wins 
from (select m.set, sum(m.points_team_a) as team_a, sum(m.points_team_b) as team_b 
     from moves m 
     where m.match_uuid = '26d41fc6-13d3-4af7-a5a3-3ec21bf06f03' 
     group by set 
    ) m 
+0

是的,謝謝! – zemirco