Ranking teams equal on points in a pool based on who won the game they played

后端 未结 3 593
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 02:09

I\'m writing an application that calculates the ranking of teams in the pool stages of a rugby competition (Rugby World Cup 2015, but it could apply to many other pool-based

相关标签:
3条回答
  • 2021-01-21 02:29

    This is complicated, but I would try the following.

    1. Make a view each combination of opponents and who wins the tie.
    2. With the results of your current ranking add in a column for the prior player, a column for prior rank, a column for next rank using windowing functions
    3. Join to the view in step 1
    4. Make a case statement that only sets a value if prior rank or next rank is equal that has the value from the view in step 1.
    5. Sort by rank and the column in step 4
    0 讨论(0)
  • 2021-01-21 02:31

    This need sql 2012+ using LEAD() and LAG() functions, Also require only two team have same Rank at this moment.

    SCHEMA

    CREATE TABLE Table1
        ([team] varchar(1), [rank] int);
    
    INSERT INTO Table1
        ([team], [rank])
    VALUES
        ('A', 1),('B', 1),('C', 2);
    
    CREATE TABLE Table2
        ([team1] varchar(1), [team2] varchar(1), [win] varchar(1));
    
    INSERT INTO Table2
        ([team1], [team2], [win])
    VALUES
        ('A', 'B', 'B'), ('C', 'A', 'A'),('C', 'B', 'B');
    

    SQL Fiddle Demo

    WITH breakTie AS ( 
        SELECT
            [team],
            [rank],
            LAG([team]) OVER (ORDER BY [rank]) PreviousTeam,
            LEAD([team]) OVER (ORDER BY [rank]) NextTeam,
            LAG([rank]) OVER (ORDER BY [rank]) PreviousRank,
            LEAD([rank]) OVER (ORDER BY [rank]) NextRank
        FROM Table1
    )
    SELECT *, CASE 
           WHEN B.[rank] = B.[NextRank] and B.[team] = T.[win] THEN 1
           WHEN B.[rank] = B.[PreviousRank] and B.[team] = T.[win] THEN 1
           ELSE 0
        END as breakT
    FROM breakTie B
    LEFT JOIN Table2 T
       ON ( B.team = T.team1 or B.team = T.team2)
      AND ( B.NextTeam = T.team1 or B.NextTeam = T.team2)
    ORDER BY 
        [rank],
        CASE 
           WHEN B.[rank] = B.[NextRank] and B.[team] = T.[win] THEN 1
           WHEN B.[rank] = B.[PreviousRank] and B.[team] = T.[win] THEN 1
           ELSE 0
        END
    
    0 讨论(0)
  • 2021-01-21 02:46

    I want to add some other suggestion. Because your current problem have some contradictions and may not have a proper answer with current rules.

    • First picture 4 team tie in same rank. If you use the information of the first 4 rows. You get a positions D, A, B, C but then last two can't be enforce.
      • In NFL they have some tie break called inside conference win. So you could count how many win each team have inside this group as a way of order.
      • So now all condition are pair comparasion.
      • In this case A and D have more wins and D beat A.
      • Now C beat B and A beat C. And you have a real order.
    • But as show in picture 2 you can have everyone tie with 2 win.

    My guess is you should analyze the restrictions. Maybe the order of rules is different and you can reduce THIS problem to one where only two team are tie.

    0 讨论(0)
提交回复
热议问题