Creating a round robin table with SQL & VB

  • I am trying to create a dynamic round robin scoring sheet with SQL stored procedure & VB and suggestion on how to do this?

    Some weekend we may have 6 players and other weekend we may have 12 players.

    If you have a script may help to do this will be greatful.

  • If I understand correctly, you simply want it where all teams play all the other teams? Here's one way to do that...

    --===== Declare and set the number of players present.

    -- This could be a parameter in a stored procedure or function.

    DECLARE @NumberOfPlayers INT;

    SELECT @NumberOfPlayers = 7; --Obviously, there will be a "BYE" for the 8th position.

    --===== If the number of players is an odd number, add 1 to the number of players

    SELECT @NumberOfPlayers = @NumberOfPlayers + (@NumberOfPlayers %2);

    --===== Create the "Round Robin" schedule using a Triangular Join (a limited form of CROSS JOIN).

    -- A better way to do this would be to use a Tally Table instead of master.dbo.spt_values.

    -- Please see the following article for that.

    -- http://www.sqlservercentral.com/articles/T-SQL/62867/

    WITH

    cteTally AS

    (

    SELECT PlayerNumber = Number

    FROM master.dbo.spt_values

    WHERE Type = 'P'

    AND Number BETWEEN 1 AND @NumberOfPlayers

    )

    SELECT t1.PlayerNumber, t2.PlayerNumber

    FROM cteTally t1

    INNER JOIN cteTally t2

    ON t1.PlayerNumber < t2.PlayerNumber

    ;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Actually, looking back at it, that does you no good because it's not actually a schedule. :blush: It doesn't contain "rounds" nor "fields of play". I'm working on something better. I'm not sure how long it will take but I have the math worked out and I'm working on the code.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Well, I thought I had the math worked out. :blush: My apologies. Creating the schedule is a bit tougher than I thought (correctly rotating the field of play is the hard part). I don't have a way to do that yet.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Creating a round robin scoring grid is tougher than I thought...

  • I can create a Round Robin that shows the weekly schedule or just a schedule for a day in SQL and in a set based fashion. I just haven't found a way to "jostle" things in a set based fashion so that thing work out like they might on a bowling league schedule which moves the "fixed point" across lanes each week (which also has some serious positional flaws for lower numbers of teams).

    Are you interested in just being able to produce the schedule without proverbial "lane assignments"?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I am always willing to learn new method or idea. I think I am come to realization that how difficult to assign lanes.

    Is it possible to create a grid something like to keep track of scoring?

    x12345

    1x----

    2-x---

    3--x--

    4---x-

    5----x

    count player & tmp table idea...

  • hi there,

    i know about round robin because of chess tournaments.

    it is like everybody against everybody, but in chess is double turn because once you play as white and one more time as black with every single opponent.

    i know round robin algorithms exist.

    there are also round robin schheduling algorithms.

    i think you should look at one already made, but maybe in another programming language.

    please, look at these web searches:

    http://www.google.com/#hl=en&sugexp=pfwl&cp=25&gs_id=au&xhr=t&q=round+robin+tournament+algorithm&pq=round+robin+algorithm+sql&pf=p&sclient=psy-ab&pbx=1&oq=round+robin+tournament+al&aq=0&aqi=g1g-v2g-b1&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=5895f996020e53d9&biw=1280&bih=899

    http://www.google.com/#hl=en&sclient=psy-ab&q=round+robin+scheduling+tournament+algorithm&pbx=1&oq=round+robin+scheduling+tournament+algorithm&aq=f&aqi=&aql=&gs_sm=e&gs_upl=13589l15669l8l16567l11l10l0l0l0l9l1009l4872l2-1.0.4.3.0.1l9l0&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=5895f996020e53d9&biw=1280&bih=899

    and maybe, if you are lucky enough, here might be lying your answer:

    http://www.google.com/#hl=en&sugexp=pfwl&cp=28&gs_id=fy&xhr=t&q=round+robin+algorithm+in+sql&pq=round+robin+scheduling+tournament+algorithm&pf=p&sclient=psy-ab&pbx=1&oq=round+robin+algorithm+in+sql&aq=f&aqi=&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=5895f996020e53d9&biw=1280&bih=899

    the actual searches are:

    round robin tournament algorithm

    round robin scheduling algorithm

    these two are actually google saved searches

    but this one is mine

    round robin algorithm sql

    good luck,

    tonci korsano.

  • Dang it! I'm sorry. I lost track of this thread. :blush:

    I said I had worked out the math for a Round Robin schedule but not the "Field of Play" rotations like you might need for a bowling league. Then I lost track of this thread.

    If you'll forgive me for being a week late, I converted the math that I had worked out to a T-SQL algorithm for solving Round Robin scheduling for virtually any number of teams you may have. It does use the magic of a Tally Table and you can find out how it works to replace certain types of loops at the following URL:

    http://www.sqlservercentral.com/articles/T-SQL/62867/

    This particular code uses a "zero based" Tally Table. You can easily build such a thing using the following code...

    --===== Do this in a nice safe place that everyone has

    USE TempDB;

    IF OBJECT_ID('TempDB..Tally','U') IS NOT NULL

    DROP TABLE Tally;

    GO

    --===================================================================

    -- Create a Tally table from 0 to 11000

    --===================================================================

    --===== Create and populate the Tally table on the fly.

    SELECT TOP 11001

    IDENTITY(INT,0,1) AS N

    INTO dbo.Tally

    FROM Master.sys.ALL_Columns ac1

    CROSS JOIN Master.sys.ALL_Columns ac2

    ;

    --===== Add a CLUSTERED Primary Key to maximize performance

    ALTER TABLE dbo.Tally

    ADD CONSTRAINT PK_Tally_N

    PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100

    ;

    --===== Allow the general public to use it

    GRANT SELECT ON dbo.Tally TO PUBLIC

    ;

    GO

    Here's the code to create the "Round Robin" schedules using T-SQL. If you really want it to "go horizontal", let me know and I'll write the necessary dynamic SQL to do so.

    /**********************************************************************************************************************

    Purpose:

    Given virtually any number of teams > 0, create a "Round Robin" schedule.

    Notes:

    1. This code does NOT cycle the last team through "Match" positions (which could be lane pairs for a bowling league).

    Separate code would be need for that.

    2. This code could be converted to an function or a stored procedure.

    Revision History:

    Rev 00 - 17 Jan 2012 - Jeff Moden

    **********************************************************************************************************************/

    --===== Do this in a nice safe place that everyone has

    USE TempDB;

    --===== Declare and set the number of players present.

    -- This could be a parameter in a stored procedure or function.

    DECLARE @Teams INT;

    SELECT @Teams = 6; --<<< This could be a parameter in a function or a proc

    --===== Conditionally drop the working table

    IF OBJECT_ID('tempdb..#Team','U') IS NOT NULL

    DROP TABLE #Team

    ;

    --===== If the number of players is an odd number, add 1 to the number of players

    SELECT @Teams = @Teams + (@Teams % 2)

    ;

    --===== Create the team/reference table table with a preset "Reference" to act as a "ring counter"

    SELECT Team = t.N,

    Reference = CASE --This is a zero based number

    WHEN t.N = @Teams THEN @Teams - 1 --Last position is always last count

    WHEN t.N % 2 = 1 THEN t.N/2 --Count odd forwards

    WHEN t.N % 2 = 0 THEN (@Teams-t.N)/2+(@Teams/2)-1 --Count all but last even backwards

    END,

    Match = (t.N - 1) / 2 + 1

    INTO #Team

    FROM dbo.Tally t

    WHERE t.N >= 1 AND t.N <= @Teams

    ;

    --===== Produce the "Round Robin" schedule

    WITH

    cteBuildRounds AS

    ( --=== This builds the round information

    SELECT [Round] = t.N + 1,

    Reference = CASE

    WHEN robin.Reference < (@Teams - 1) THEN (robin.Reference + t.N) % (@Teams - 1)

    ELSE robin.Reference

    END,

    Match = robin.Match,

    Side = (robin.Team - 1) % 2

    FROM #Team robin

    CROSS JOIN dbo.Tally t

    WHERE t.N BETWEEN 0 AND (@Teams - 2)

    ) --=== This simply summarizes by match

    SELECT br.[Round],

    br.Match,

    Teams = MAX(CASE WHEN br.Side = 0 THEN CAST(team.Team AS VARCHAR(10)) ELSE '' END) + ' - '

    + MAX(CASE WHEN br.Side = 1 THEN CAST(team.Team AS VARCHAR(10)) ELSE '' END)

    FROM cteBuildRounds br

    INNER JOIN #Team team

    ON team.Reference = br.Reference

    GROUP BY br.[Round], br.Match

    ORDER BY br.[Round], br.Match

    ;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply