2013-12-08 141 views
1

我想這取決於條件,如果player_id(加里) 具有大於,等於或小於得分,然後player_id(「其他」)聚合條件?

我的架構具有

players(player_id, name) 

matches(match_id, home_team(player_id), away_team(player_id)) 

outcome(outcome_id, match_id, home_score:integer, away_score:integer 

輸出彙總:

select m.match_id, p.name AS home_team, p1.name AS away_team, o.home_score, o.away_score 
from players p 
inner join matches m on (p.player_id = m.home_team) 
inner join players p1 on (p1.player_id = m.away_team) 
inner join outcomes o on (m.match_id = o.match_id); 

match_id | player_id | player_id | home_score | away_score 
----------+-----------+-----------+------------+------------ 
     1 | 1   | 2   |   1 |   2 
     2 | 2   | 1   |   1 |   3 
     3 | 3   | 1   |   3 |   2 

求購輸出:

player_id | Wins | Draws | Losses 
-------------+------+-------+-------- 
    1   | 1 | 0 | 2 
    2 ... | ... | .. | ... 

我的模式是可以修改的。

EDIT(sqlfiddle):http://www.sqlfiddle.com/#!2/7b6c8/1

+0

啊遺憾的含混,這事doesent!重點更多的是贏得每場比賽的勝利和抽獎次數@Filipe Silva – skyw00lker

+1

您可以在您的原始表格樣本數據中添加[sqlfiddle](http://www.sqlfiddle.com)嗎? –

+0

會做! @FilipeSilva – skyw00lker

回答

1

我會用UNION ALL讓每一個outcome兩次,一次爲home,一次爲away玩家。應該切換第二次home_score/away_score,以獲得away玩家的正確金額。

select 
    d.player_id, 
    d.name, 
    sum(d.home_score > d.away_score) as wins, 
    sum(d.home_score = d.away_score) as draws, 
    sum(d.home_score < d.away_score) as loses 
from (
    select p.player_id, p.name, o.home_score, o.away_score 
    from players p 
    join matches m on p.player_id = m.home_team 
    join outcomes o on o.match_id = m.match_id 
    union all 
    select p.player_id, p.name, o.away_score as home_score, o.home_score as away_score 
    from players p 
    join matches m on p.player_id = m.away_team 
    join outcomes o on o.match_id = m.match_id) d 
group by d.player_id, d.name 

返回:

PLAYER_ID NAME WINS DRAWS LOSES 
1   Gary 1  0  2 
2   Tom  1  0  1 
3   Brad 1  0  0 

sqlFiddle演示:http://www.sqlfiddle.com/#!2/7b6c8/21

+0

非常感謝!這非常有幫助,並得到了預期的結果,但我不知道我的模式是否正確設置了回答這種查詢! @MarcinJuraszek – skyw00lker

1

對於一個解決方案,而子查詢和工會:http://www.sqlfiddle.com/#!2/7b6c8/31

SELECT 
    p.player_id, 
    COALESCE(SUM(o1.home_score > o1.away_score or o2.home_score < o2.away_score), 0) wins, 
    COALESCE(SUM(o1.home_score = o1.away_score or o2.home_score = o2.away_score), 0) draws, 
    COALESCE(SUM(o1.home_score < o1.away_score or o2.home_score > o2.away_score), 0) losses 
FROM players p 
LEFT JOIN matches m1 ON (p.player_id = m1.home_team) 
LEFT JOIN players p1 ON (p1.player_id = m1.away_team) 
LEFT JOIN outcomes o1 ON (m1.match_id = o1.match_id) 
LEFT JOIN matches m2 ON (p.player_id = m2.away_team) 
LEFT JOIN players p2 ON (p2.player_id = m2.home_team) 
LEFT JOIN outcomes o2 ON (m2.match_id = o2.match_id) 

GROUP BY p.player_id  

結果:

PLAYER_ID WINS DRAWS LOSSES 
1 1 0 2 
2 1 0 1 
3 1 0 0 
4 0 0 0 
5 0 0 0 
+0

這不會像預期的那樣返回結果恐怕應該是Marcin的answare @Wolph – skyw00lker

+0

的確,我忘了反向關係:)我已經添加了一個修改版本。我個人不是聯盟的粉絲,所有的方法 – Wolph

+0

啊好!謝謝你的解決方案,我從這兩個例子中學到了很多! 我想把你的answare作爲解決方案,但marcin解決了它「更快」,在製作中,我想我會和你一起去,儘管@Wolph – skyw00lker