SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating a round robin table with SQL & VB


Creating a round robin table with SQL & VB

Author
Message
Aloha
Aloha
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 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.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84695 Visits: 41069
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84695 Visits: 41069
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84695 Visits: 41069
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Aloha
Aloha
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 Visits: 30
Creating a round robin scoring grid is tougher than I thought...
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84695 Visits: 41069
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Aloha
Aloha
Grasshopper
Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)Grasshopper (24 reputation)

Group: General Forum Members
Points: 24 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...
Tonci Korsano
Tonci Korsano
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 46
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.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84695 Visits: 41069
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search