Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Creating a round robin table with SQL & VB Expand / Collapse
Author
Message
Posted Saturday, January 7, 2012 8:19 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, July 13, 2013 2:49 PM
Points: 10, Visits: 30
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.
Post #1231899
Posted Saturday, January 7, 2012 12:43 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1231963
Posted Saturday, January 7, 2012 3:33 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
Actually, looking back at it, that does you no good because it's not actually a schedule. 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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1231977
Posted Saturday, January 7, 2012 5:59 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
Well, I thought I had the math worked out. 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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1231997
Posted Monday, January 9, 2012 9:05 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, July 13, 2013 2:49 PM
Points: 10, Visits: 30
Creating a round robin scoring grid is tougher than I thought...
Post #1232530
Posted Monday, January 9, 2012 10:05 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1232906
Posted Monday, January 9, 2012 10:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, July 13, 2013 2:49 PM
Points: 10, Visits: 30
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...
Post #1232917
Posted Wednesday, January 11, 2012 11:02 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 16, 2013 8:18 AM
Points: 15, Visits: 45
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.
Post #1234216
Posted Tuesday, January 17, 2012 9:14 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:47 PM
Points: 35,215, Visits: 31,667
Dang it! I'm sorry. I lost track of this thread.

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

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1237652
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse