2017-02-01 76 views
1

從最近的一次訪談中發現了這個問題。SQL根據匹配對隊伍進行排名

兩個表

create table teams { 
    team_id, 
    team_name 
}; 

create table matches { 
    match_id, 
    host_team, 
    guest_team, 
    host_goals, 
    guest_goals 
}; 

的打分規則是:贏= 3分,畫出= 1分,失去= 0分。

如何編寫一個SQL查詢來根據他們的分數對所有團隊進行排名?

例如

team_id | team_name 
    ---------+--------------- 
    10  | A 
    20  | B 
    30  | C 
    40  | D 
    50  | E 

比賽

match_id | host_team | guest_team | host_goals | guest_goals 
    ----------+-----------+------------+------------+------------- 
    1  | 30  | 20   | 1   | 0 
    2  | 10  | 20   | 1   | 2 
    3  | 20  | 50   | 2   | 2 
    4  | 10  | 30   | 1   | 0 
    5  | 30  | 50   | 0   | 1 

查詢應該返回

team_id | team_name | num_points 
    ---------+-----------+------------ 
    20  | B   | 4 
    50  | E   | 4 
    10  | A   | 3 
    30  | C   | 3 
    40  | D   | 0 
+3

你一定嘗試過什麼嗎? –

+0

@GordonLinoff SQL並不是我的強項,我很肯定我的面試失敗主要是因爲這個問題。只是想得到一些想法。 – ddd

+0

可以顯示樣本輸入數據和預期結果 – Saif

回答

1

我會這樣跟UNION,像這樣的(請注意,您必須使用UNION,不這個問題的UNION ALL):

SELECT host_team, 3 as points 
FROM matches where host_goals > guest_goals 
UNION 
SELECT guest_team, 3 as points 
FROM matches where host_goals < guest_goals 
UNION 
SELECT host_team, 1 as points 
FROM matches where host_goals = guest_goals 
UNION 
SELECT guest_team, 1 as points 
FROM matches where host_goals = guest_goals 

在上面的查詢中,我使用了兩次繪製的SELECTs,這樣兩個團隊都可以獲得一個積分。

而現在,所有你需要做的是聚集的結果,並與球隊表連接它來獲得球隊的名字:

;with results as(
    SELECT host_team as team_id, 3 as points 
    FROM matches where host_goals > guest_goals 
    UNION 
    SELECT guest_team as team_id, 3 as points 
    FROM matches where host_goals < guest_goals 
    UNION 
    SELECT host_team as team_id, 1 as points 
    FROM matches where host_goals = guest_goals 
    UNION 
    SELECT guest_team as team_id, 1 as points 
    FROM matches where host_goals = guest_goals 
), totals as (
    SELECT team_id, SUM(points) as num_points 
    from results 
    GROUP BY team_id 
) 
SELECT t.team_id, t.team_name, COALESCE(totals.num_points, 0) as num_points 
FROM teams t LEFT JOIN totals 
ON t.team_id = totals.team_id 
2

嘗試下面的腳本

select t.team_id 
     ,t.team_name 
     ,isnull(SUM(points),0) as num_points 
from teams as t 
    left join (
    select host_team as team 
      ,(case when host_goals > guest_goals then 3 
       when host_goals = guest_goals then 1 
       else 0 end) as points 
    from matches 
    union 
    select guest_team as team 
      ,(case when guest_goals > host_goals then 3 
       when guest_goals = host_goals then 1 
       else 0 end) as points 
    from matches 
    ) as d on d.team = t.team_id 
group by t.team_id 
     ,t.team_name 
0

你會需要以編程方式進行,不知道使用什麼語言就不能提供很多幫助。

可能還需要更多的表格字段。

create table teams { 
    team_id, 
    team_name, 
    team_wins, 
    team_losses, 
    team_ranks 
}; 

create table matches { 
    match_id, 
    host_team_id, 
    guest_team_id, 
    host_goals, 
    guest_goals 
}; 

所以首先爲每個匹配計算主機的目標和來賓的目標,然後加入球隊的名字,這取決於分數贏家和輸家列。

然後計算每支球隊的勝率,並根據勝利數或贏/勝比分配等級。

相關問題