Good Day. I have a new Apex database For a dart league.
I have a lot of work to do! but cant combine (with code - I have the right numbers on the interactive grid by using the sum function) I have the following code and results that work for each player, which I need, But cannot get the summed of average for each player.... this matters when creating a handicap.
select Team, name,
COUNT(WEEK) * 3 GAMES,
sum(game_1) + sum(game_2) + sum(game_3) points,
Round(((sum(game_1) + sum(game_2) + sum(game_3)) / ((COUNT(WEEK) * 3))), 2) Average
from score_tbl
group by Team, name
order by 1
TEAM
NAME
GAMES
POINTS
AVERAGE
1
B Tyler
6
142
23.67
1
Blind
6
108
18
1
Jim V
6
53
8.83
1
KC M
6
82
13.67
2
J Spass
6
102
17
2
Randy B
6
105
17.5
2
Tim Ketz
6
74
12.33
2
Todd Lapan
6
51
8.5
I am trying to figure out the code to sum the Averages for each player by team.
Team Average Handicap
Team 1 64.17
Team 2 55.33
etc..
then, if possible compare those averages to find the highest average. Then take the Highest avg - (each Team) * 90%.
I am trying to figure out the code to sum the Averages for each player by team.
Wrap the expression for generating the average in an analytic SUM for each team partition:
SELECT team,
name,
COUNT(WEEK) * 3 AS games,
SUM(game_1 + game_2 + game_3) AS points,
ROUND(AVG(game_1 + game_2 + game_3) / 3, 2) AS average,
ROUND(SUM(AVG(game_1 + game_2 + game_3) / 3) OVER (PARTITION BY team), 2)
AS total_average
FROM score_tbl
GROUP BY
team, name
ORDER BY
team;
Which, for the sample data:
CREATE TABLE score_tbl(team, name, week, game_1, game_2, game_3) AS
SELECT 1, 'B Tyler', 1, 24, 24, 23 FROM DUAL UNION ALL
SELECT 1, 'Blind', 1, 18, 18, 18 FROM DUAL UNION ALL
SELECT 1, 'Jim V', 1, 9, 8, 9.5 FROM DUAL UNION ALL
SELECT 1, 'KC M', 1, 14, 14, 13 FROM DUAL UNION ALL
SELECT 2, 'J Spass', 1, 17, 17, 17 FROM DUAL UNION ALL
SELECT 2, 'Randy B', 1, 18, 17, 17.5 FROM DUAL UNION ALL
SELECT 2, 'Tim Ketz', 1, 13, 12, 12 FROM DUAL UNION ALL
SELECT 2, 'Todd Lapan', 1, 9, 8, 8.5 FROM DUAL UNION ALL
SELECT 1, 'B Tyler', 1, 24, 24, 23 FROM DUAL UNION ALL
SELECT 1, 'Blind', 1, 18, 18, 18 FROM DUAL UNION ALL
SELECT 1, 'Jim V', 1, 9, 8, 9.5 FROM DUAL UNION ALL
SELECT 1, 'KC M', 1, 14, 14, 13 FROM DUAL UNION ALL
SELECT 2, 'J Spass', 1, 17, 17, 17 FROM DUAL UNION ALL
SELECT 2, 'Randy B', 1, 18, 17, 17.5 FROM DUAL UNION ALL
SELECT 2, 'Tim Ketz', 1, 13, 12, 12 FROM DUAL UNION ALL
SELECT 2, 'Todd Lapan', 1, 9, 8, 8.5 FROM DUAL;
Outputs:
TEAM
NAME
GAMES
POINTS
AVERAGE
TOTAL_AVERAGE
1
B Tyler
6
142
23.67
64.17
1
Blind
6
108
18
64.17
1
Jim V
6
53
8.83
64.17
1
KC M
6
82
13.67
64.17
2
J Spass
6
102
17
55.33
2
Randy B
6
105
17.5
55.33
2
Tim Ketz
6
74
12.33
55.33
2
Todd Lapan
6
51
8.5
55.33
fiddle
I want to find all solutions for this "For a given interval of years to get the number of days per week(mon, tue, web,...,sun) of the year"
does anybody know other answer? share it please ;)
this is my query:)
WITH dat AS
(SELECT &&start_year + level - 1 cur_year, DECODE(MOD(&start_year + level - 1, 4), 0, 0, 1) no_leap,
TO_NUMBER(TO_CHAR(TO_DATE('01.01.'||TO_CHAR(&start_year + level - 1)), 'D')) d
FROM dual
CONNECT BY LEVEL < &end_year - &start_year + 2)
SELECT cur_year,
51 + DECODE(no_leap, 1, DECODE(d, 1, 2, 1), DECODE(d, 1, 2, 7, 2, 1)) mon,
51 + DECODE(no_leap, 1, DECODE(d, 2, 2, 1), DECODE(d, 2, 2, 1, 2, 1)) tue,
51 + DECODE(no_leap, 1, DECODE(d, 3, 2, 1), DECODE(d, 3, 2, 2, 2, 1)) wed,
51 + DECODE(no_leap, 1, DECODE(d, 4, 2, 1), DECODE(d, 4, 2, 3, 2, 1)) thu,
51 + DECODE(no_leap, 1, DECODE(d, 5, 2, 1), DECODE(d, 5, 2, 4, 2, 1)) fri,
51 + DECODE(no_leap, 1, DECODE(d, 6, 2, 1), DECODE(d, 6, 2, 5, 2, 1)) sat,
51 + DECODE(no_leap, 1, DECODE(d, 7, 2, 1), DECODE(d, 7, 2, 6, 2, 1)) sun
FROM dat;
You should be taking advantage of Oracle's date-arithmetic. When you minus two dates from each other a number is returned, the number of days between the two dates. If there's not an integer difference then the actual difference is returned:
SQL> select sysdate - to_date('20120504 15', 'yyyymmdd hh24') from dual;
SYSDATE-TO_DATE('2012050415','YYYYMMDDHH24')
--------------------------------------------
3.76489583
Please note that this does not include timestamp, only date data-types. Doing timestamp arithmetic returns an interval data-type.
Most of a solution would be
with dates as (
select to_char(to_date('&start_year','yyyy') + level, 'fmDAY') as days
from dual
connect by level < to_date('&end_year','yyyy') - to_date('&start_year','yyyy')
)
select ...
Note the fm modifier of the format model; this removes trailing spaces.
As a general rule, when dealing with dates it is always preferable to use dates, rather than numbers or characters. Your connect by clause CONNECT BY LEVEL < &end_year - &start_year + 2, though simple has caused you to do a lot of extra work in order to get your solution to work.
I need to produce the schedule of a sport-event.
There are 30 teams. Each team has to play 8 matches. This means that it is not possible for each team to compete again all other teams, but I need to avoid that two team compete more than once against each other.
My idea was to generate all possible matches (for 30 teams: (30*29)/2 = 435 matches) and select from this list 120 matches (8 match for each team: 8 * 30 / 2 = 120 matches).
This is where I'm having a hard time: how can I select these 120 matches? I tried some simple solutions (take first match of the list, then the last, and so on) but they don't seem to work with 30 teams. I also tried to generate all possible match combination and find which one is working but with 30 team, this is too much calculation time.
Is there an existing algorithm that I could implement?
UPDATE
What I need to produce is a simple schedule, without elimination. Each team play 8 matchs, and that's all. At the end of the day there won't be one winner.
Each team will have his schedule, and this schedule won't change wether they win or lose. The planning is done for the whole day and is immutable.
UPDATE 2
At first, I didn't want to put too many constraints to my question, but it seems that without any constraints (other than each team not competing more than once with each other), it's just a matter of random picking 8 matchs for each team.
So here is some more details :
During this sport event, there are 6 differents sports (soccer, handball, basketball, and so on). This means there are 6 simultaneous matchs. A new round is started every 15 minutes.
Each team will have to play 8 matches, and each sport at least once.
These 6 sports are taking place at three different places. This means that during the day, each team will have to move from one place to another. These moves should be reduced as much as possible.
A team cannot play two matches in a row.
You could look into some already known matching approaches:
E.g. Swiss Chess system
Edit:
After reading your requirements again - that every team should play every other team exactly once, and that a winner need not necessarily be decided. It seems like a single Round Robin system would do what you want. You could just drop any extra matchups above the 8 you need.
It's quite simple really, just team up team i with i-4, i-3, i-2, i-1, i+1, i+2, i+3, i+4. This can be done using the algorithm below.
import java.util.*;
public class Test {
public static void main(String[] args) {
int TEAMS = 30, MATCHES = 8;
int[] matchCount = new int[TEAMS]; // for a sanity check.
List<Match> matches = new ArrayList<Match>();
for (int team1 = 0; team1 < TEAMS; team1++)
for (int team2 = team1 + 1; team2 <= team1 + MATCHES/2; team2++) {
matches.add(new Match(team1, team2 % TEAMS));
// Just for a sanity check:
matchCount[team1]++;
matchCount[team2 % TEAMS]++;
}
System.out.println(matches);
// Sanity check:
System.out.println(matches.size());
System.out.println(Arrays.toString(matchCount));
}
static class Match {
int team1, team2;
public Match(int team1, int team2) {
this.team1 = team1;
this.team2 = team2;
}
public String toString() {
return team1 + " vs " + team2;
}
}
}
Output:
[0 vs 1, 0 vs 2, 0 vs 3, 0 vs 4, 1 vs 2, 1 vs 3, 1 vs 4, 1 vs 5, 2 vs 3, 2 vs 4, 2 vs 5, 2 vs 6, 3 vs 4, 3 vs 5, 3 vs 6, 3 vs 7, 4 vs 5, 4 vs 6, 4 vs 7, 4 vs 8, 5 vs 6, 5 vs 7, 5 vs 8, 5 vs 9, 6 vs 7, 6 vs 8, 6 vs 9, 6 vs 10, 7 vs 8, 7 vs 9, 7 vs 10, 7 vs 11, 8 vs 9, 8 vs 10, 8 vs 11, 8 vs 12, 9 vs 10, 9 vs 11, 9 vs 12, 9 vs 13, 10 vs 11, 10 vs 12, 10 vs 13, 10 vs 14, 11 vs 12, 11 vs 13, 11 vs 14, 11 vs 15, 12 vs 13, 12 vs 14, 12 vs 15, 12 vs 16, 13 vs 14, 13 vs 15, 13 vs 16, 13 vs 17, 14 vs 15, 14 vs 16, 14 vs 17, 14 vs 18, 15 vs 16, 15 vs 17, 15 vs 18, 15 vs 19, 16 vs 17, 16 vs 18, 16 vs 19, 16 vs 20, 17 vs 18, 17 vs 19, 17 vs 20, 17 vs 21, 18 vs 19, 18 vs 20, 18 vs 21, 18 vs 22, 19 vs 20, 19 vs 21, 19 vs 22, 19 vs 23, 20 vs 21, 20 vs 22, 20 vs 23, 20 vs 24, 21 vs 22, 21 vs 23, 21 vs 24, 21 vs 25, 22 vs 23, 22 vs 24, 22 vs 25, 22 vs 26, 23 vs 24, 23 vs 25, 23 vs 26, 23 vs 27, 24 vs 25, 24 vs 26, 24 vs 27, 24 vs 28, 25 vs 26, 25 vs 27, 25 vs 28, 25 vs 29, 26 vs 27, 26 vs 28, 26 vs 29, 26 vs 0, 27 vs 28, 27 vs 29, 27 vs 0, 27 vs 1, 28 vs 29, 28 vs 0, 28 vs 1, 28 vs 2, 29 vs 0, 29 vs 1, 29 vs 2, 29 vs 3]
120
[8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]
If you would like a more randomized setup, you could simply assign a random number between 1 and 30 to each team.
Update To cope with your added constraints: Let match i be of sport i mod 6.
Are you sure you couldn't get 32 teams :-) ?
That would make things simpler - have a standard tournament structure, but have the losers from each round play in their own chart.
I think this maximises the number of teams who win at least one match during the event.
With 30 teams, you could have 2 teams play a 'friendly' and have a by in the first round. But the organisation becomes much more complicated.
I do not know of an existing implementation, but here is a possible solution for you:
Make three groups of 9 teams and pair them each with all others, so that each plays once against all others. Each of the 27 teams now plays 8 games.
Now take the remaining three teams, one for each group.
Modify some games of each team:
1-2 -> 1-10, 2-10
3-4 -> 3-10, 4-10
5-6 -> 5-10, 6-10
7-8 -> 7-10, 8-10
Broker's Algorithm might work. Funnily enough I can't find a good description in the net, so I'll try to explain the system.
Effectively, each team would ask each other team for a score for a match and the matchup that scores highest is selected. This is repeated for each team and match. The resulting schedule is saved and a total score is calculated. Then with different starting points (i.e. you could just randomise the team order) this is done again and if the total score is higher, this schedule is selected instead. You repeat this until you either find a schedule yielding a high enought score or after a pre-defined number of tries.
Total score could be calculated by distance travelled by each team, the smaller the number the better. Obviously you don't pick matches that break your rules (too many matches of similar type, same teams playing each other again).
Scoring could go something like this:
If same venue for team: 100 points (i.e. if for both = 200).
For travelling between venues, score is determined for both teams depending on length, i.e. A -> B and B -> A 50 points (they are near) A -> C and C -> A 30 points (not so near)
B -> C and C -> B 0 points (the travelling we want to do as little as possible).
If team hasn't played this sports yet: 100 points.
If team has played this sport once: 50 points.
Of course the problem with BA is finding good values for different parameters so you would need to spend some time finding them.
If you want a simple algorithm that will produce a schedule where teams don't play against each other more than once that is not hard with given parameters.
Here is an example for 10 teams and 5 rounds, the solution is shown as an array where if a value of schedule[i][j] is zero the teams don't play together, and if it is a number then it shows in which round they play together.
1 2 3 4 5 6 7 8 9 10
1 [0, 5, 0, 4, 0, 3, 0, 2, 0, 1]
2 [5, 0, 4, 0, 3, 0, 2, 0, 1, 0]
3 [0, 4, 0, 3, 0, 2, 0, 1, 0, 5]
4 [4, 0, 3, 0, 2, 0, 1, 0, 5, 0]
5 [0, 3, 0, 2, 0, 1, 0, 5, 0, 4]
6 [3, 0, 2, 0, 1, 0, 5, 0, 4, 0]
7 [0, 2, 0, 1, 0, 5, 0, 4, 0, 3]
8 [2, 0, 1, 0, 5, 0, 4, 0, 3, 0]
9 [0, 1, 0, 5, 0, 4, 0, 3, 0, 2]
10[1, 0, 5, 0, 4, 0, 3, 0, 2, 0]
So from this table in first round the teams (1, 10), (2, 9), (3, 8), (4, 7), (5, 6) play, in the second round the teams (1, 8), (2, 7), (3, 6)... etc.
To produce this table the algorithm is rather trivial, here is some python code:
#!/bin/env python
def simpleNRooks(size, rounds, schedule):
''' Place n rooks on board so that they don't hit each other in each round,
nor reuse the spots from previous rounds '''
for i in range(size):
for j in range(rounds):
if size-j*2-i-1 < 0:
schedule[i][2*size-j*2-i-1] = j + 1
else:
schedule[i][size-j*2-i-1] = j + 1
# parameters
teams = 10
matches = 5
# prepare the schedule, 0's designate free space
schedule = [[0 for i in range(teams)] for j in range(teams)]
simpleNRooks(teams, matches, schedule)
print 'Final schedule'
for i in range(teams):
print schedule[i]
If you want to get a different data structure out (for example a list of pairs by rounds) you can use the same principle, but change the loops.
I'm adding a separate answer given the new constraints, as I think it's clearer than adding to my old answer.
Split the 30 teams into 5 groups each of 6 teams: A B C D E
For the first period Group A and B play.
Then C&D, E&A, B&C, D&E, for the next 4 fifteen minute segments.
So at the end of 5 * 15 minutes: Every team has played twice, with at least one rest period between goes.
Have 20 periods and everyone has played 8 times.
It should be easy to allow a team in group B, for example, to play against 8 of the other teams from the 17 other teams in A,B & C groups.
For example play A teams against matching B teams, then later, B teams against matching C teams, then reverse lists, then within groups, then MOD 2, MOD 3 between groups, and within groups.
That just leaves minimising travel time, and ensuring that every team plays every game type. But solve that for one group, and you can apply the same solution to all the other ones?
Could someone explain to me what is going on here and how to solve this problem?
Suppose relation R(A,B) has the tuples:
A B
1 2
3 4
5 6
and the relation S(B,C,D) has tuples:
B C D
2 4 6
4 6 8
4 7 9
Compute the natural join of R and S. Then, identify which of the following tuples is in the
natural join R |><| S. You may assume each tuple has schema (A,B,C,D).
I don't know what a natural join truly means. Can you explain it to me?
A natural join is joining ("sticking together") elements from two relations where there is a match. In this example
(1, 2) matches (2, 4, 6) so you get (1, 2, 4, 6)
(3, 4) matches (4, 6, 8) so you get (3, 4, 6, 8)
(3, 4) matches (4, 7, 9) so you get (3, 4, 7, 9)
So the natural join is {(1, 2, 4, 6), (3, 4, 6, 8), (3, 4, 7, 9)}
I assume R(A,B) is the master, S(B,C,D) is the detail and B is the foreign key.
SQL: select * from R, S where R.B = S.B
Then the result is:
A B C D
1 2 4 6
3 4 6 8
3 4 7 9