2017-02-01 9 views
1

最近のインタビューの1つからこの問題に遭遇しました。マッチに基づいてチームをランク付けするSQL

2つのテーブル

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 

問い合わせは(あなたがUNIONを使用しなければならないことに注意していないしてください、私はこのように、UNIONでこれを行うだろう

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 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 

上記のクエリでは、2回の抽選でSELECTを使用して、両チームに抽選のポイントが与えられました。

そして今、あなたがする必要があるすべての結果を集約し、チーム名を取得するためにチームのテーブルでそれに参加することである。

;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

以下のスクリプトをしようとするだろうそれをプログラム的に行う必要があり、どの言語が使用されているか知らなくても多くの助けを得ることができません。

さらに多くのテーブルフィールドが必要になるでしょう。

まずホストマターとゲストゴールをそれぞれ計算し、スコアに応じて勝者と敗者の列にチーム名を追加します。

各チームのチームの勝利を数え、勝敗の数、または損失/勝率に基づいてランクを割り当てます。

関連する問題