| Author |
Topic |
|
Heinz23
Yak Posting Veteran
84 Posts |
Posted - 2008-01-25 : 14:00:04
|
Hi all,I have the following Table:Season | MatchRound | Team1 | Team2 | GoalsTeam1 | GoalsTeam21.........| 1............... | ABC.... | DEF.... | 3............. | 2.......1.........| 1............... | GHI.... | JKL.... | 1............. | 1.......1.........| 2............... | DEF.... | GHI.... | 1............. | 0.......1.........| 2............... | JKL.... | ABC.... | 0............. | 2.......1.........| 3............... | ABC.... | GHI.... | 2............. | 2.......1.........| 3............... | DEF.... | JKL.... | 0............. | 0.......2.........| 1............... | ABC.... | DEF.... | 1............. | 2.......2.........| 1............... | GHI.... | JKL.... | 3............. | 1.......2.........| 2............... | DEF.... | GHI.... | 1............. | 1.......2.........| 2............... | JKL.... | ABC.... | 0............. | 2.......2.........| 3............... | ABC.... | GHI.... | 2............. | 4.......2.........| 3............... | DEF.... | JKL.... | 0............. | 1.......Now I want to create a Stored Procedure which accepts the Season as Input Parameter and then returns the following table (win = 3 points, draw = 1 point each, loss = 0 points of course) (sorry, it's a large table)Rank | Team | Won | Draw | Lost | GoalsFor | GoalsAgainst | Points | HomeMatches | HomeWon | HomeDraw | HomeLost | GoalsForHome | GoalsAgainstHome | PointsHome | AwayMatches | AwayWon | AwayDraw | AwayLost | GoalsForAway | GoalsAgainstAway | PointsAway1 | ABC | 2 | 1 | 0 | 7 | 4 | 7 | 2 | 1 | 1 | 0 | 5 | 4 | 1 | 1 | 0 | 0 | 2 | 0 | 32 .....3 .....4 .....A good example with all the columns I need could be found here: http://web.mlsnet.com/standings/ (In case they change the side I also made a screenshot here: http://img519.imageshack.us/img519/4677/2008012525ce2.png)I have no idea how to do it Of course I could download the whole season and then compute the table in VB.net but of course I would prefer to do it on the SQL-Server (2005) already as this is faster than the other solution.I would be very grateful if someone could help me with this. Many thanks in advance!! |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-25 : 15:09:52
|
| [code]DECLARE @SeasonData TABLE ( Season INT, MatchRound INT, Team1 CHAR(3), Team2 CHAR(3), GoalsTeam1 INT, GoalsTeam2 INT )INSERT @SeasonData ( Season, MatchRound, Team1, Team2, GoalsTeam1, GoalsTeam2 )SELECT 1, 1, 'ABC', 'DEF', 3, 2 UNIONSELECT 1, 1, 'GHI', 'JKL', 1, 1 UNIONSELECT 1, 2, 'DEF', 'GHI', 1, 0 UNIONSELECT 1, 2, 'JKL', 'ABC', 0, 2 UNIONSELECT 1, 3, 'ABC', 'GHI', 2, 2 UNIONSELECT 1, 3, 'DEF', 'JKL', 0, 0 UNIONSELECT 2, 1, 'ABC', 'DEF', 1, 2 UNIONSELECT 2, 1, 'GHI', 'JKL', 3, 1 UNIONSELECT 2, 2, 'DEF', 'GHI', 1, 1 UNIONSELECT 2, 2, 'JKL', 'ABC', 0, 2 UNIONSELECT 2, 3, 'ABC', 'GHI', 2, 4 UNIONSELECT 2, 3, 'DEF', 'JKL', 0, 1SELECT Season, RANK()OVER(PARTITION BY Season ORDER BY SUM(WinLoss) DESC) AS Rank, Team, SUM(CASE WHEN WinLoss = 3 THEN 1 ELSE 0 END ) AS Won, SUM(CASE WHEN WinLoss = 1 THEN 1 ELSE 0 END ) AS Draw, SUM(CASE WHEN WinLoss = 0 THEN 1 ELSE 0 END ) AS Lost, SUM(GoalsFor) AS GoalsFor, SUM(GoalsAgainst) AS GoalsAgainst, SUM(WinLoss) AS Points, SUM(CASE WHEN Location = 'Home' THEN 1 ELSE 0 END ) AS HomeMatches, SUM(CASE WHEN Location = 'Home' AND WinLoss = 3 THEN 1 ELSE 0 END ) AS HomeWon, SUM(CASE WHEN Location = 'Home' AND WinLoss = 1 THEN 1 ELSE 0 END ) AS HomeDraw, SUM(CASE WHEN Location = 'Home' AND WinLoss = 0 THEN 1 ELSE 0 END ) AS HomeLost, SUM(CASE WHEN Location = 'Home' THEN GoalsFor ELSE 0 END ) AS GoalsForHome, SUM(CASE WHEN Location = 'Home' THEN GoalsAgainst ELSE 0 END ) AS GoalsAgainstHome, SUM(CASE WHEN Location = 'Home' THEN WinLoss ELSE 0 END ) AS PointsHome, SUM(CASE WHEN Location = 'Away' THEN 1 ELSE 0 END ) AS AwayMatches, SUM(CASE WHEN Location = 'Away' AND WinLoss = 3 THEN 1 ELSE 0 END ) AS AwayWon, SUM(CASE WHEN Location = 'Away' AND WinLoss = 1 THEN 1 ELSE 0 END ) AS AwayDraw, SUM(CASE WHEN Location = 'Away' AND WinLoss = 0 THEN 1 ELSE 0 END ) AS AwayLost, SUM(CASE WHEN Location = 'Away' THEN GoalsFor ELSE 0 END ) AS GoalsForAway, SUM(CASE WHEN Location = 'Away' THEN GoalsAgainst ELSE 0 END ) AS GoalsAgainstAway, SUM(CASE WHEN Location = 'Away' THEN WinLoss ELSE 0 END ) AS PointsAwayFROM (SELECT Season, 'Home' AS Location, Team1 AS Team, SUM(CASE WHEN GoalsTeam1 > GoalsTeam2 THEN 3 WHEN GoalsTeam1 < GoalsTeam2 THEN 0 ELSE 1 END ) WinLoss, GoalsTeam1 AS GoalsFor, GoalsTeam2 AS GoalsAgainstFROM @SeasonDataGROUP BY Season, Team1, GoalsTeam1, GoalsTeam2UNIONSELECT Season, 'Away', Team2, SUM(CASE WHEN GoalsTeam1 < GoalsTeam2 THEN 3 WHEN GoalsTeam1 > GoalsTeam2 THEN 0 ELSE 1 END ), GoalsTeam2, GoalsTeam1FROM @SeasonDataGROUP BY Season, Team2, GoalsTeam2, GoalsTeam1 ) teamdataGROUP BY Season, Team[/code] |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2008-01-25 : 15:12:17
|
| This is not really too difficult, but there are some core concepts you'll need to bone up on before you write this query. You'll need to fully understand the GROUP BY clause and how that allows use to use the SUM() aggregate. To do the Rank, you'll first need to define (in business rule terms) a ranking algorithm. Depending on what you come up with, you can probably use the RANK() or one of the other ranking functions.Jayto here knows whenEDIT: ...or someone else could do it for you ... |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-25 : 15:16:15
|
quote: Originally posted by Page47 This is not really too difficult, but there are some core concepts you'll need to bone up on before you write this query. You'll need to fully understand the GROUP BY clause and how that allows use to use the SUM() aggregate. To do the Rank, you'll first need to define (in business rule terms) a ranking algorithm. Depending on what you come up with, you can probably use the RANK() or one of the other ranking functions.Jayto here knows whenEDIT: ...or someone else could do it for you ...
Notice: relate your problem to sports and you get free code. |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2008-01-25 : 15:35:43
|
| To each his own, I guess. My honeymoon period on this forum has long since past so, while I do enjoy "giving back" to the community in terms of advise and guidance, I'm way past the temptation to be the first to post a complete solution especially to such broad yet simple questions...sports or otherwise.My only hope is that the OP takes the time to digest your code (I'm not going to bother, btw, so I also hope it is at least correct).Jayto here knows when |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-25 : 15:47:00
|
quote: Originally posted by Page47 To each his own, I guess. My honeymoon period on this forum has long since past so, while I do enjoy "giving back" to the community in terms of advise and guidance, I'm way past the temptation to be the first to post a complete solution especially to such broad yet simple questions...sports or otherwise.My only hope is that the OP takes the time to digest your code (I'm not going to bother, btw, so I also hope it is at least correct).Jayto here knows when
I always make sure to test my code... and if somehow I miss a bug and I do find an error in my posted code I will go back and point it out.I suppose Ill get to a point where I wont just post the solution but rather guide the op to it... but that takes so much more effort. |
 |
|
|
Heinz23
Yak Posting Veteran
84 Posts |
Posted - 2008-01-25 : 17:27:24
|
Hi jdaman,thank you very very much for your script!! That's quite a lot, and more than I hoped, so as said thanks a lot!! And there's quite a lot which I do not really understand yet, like Partition, Rank and GroupBy. I guess I need to check Books Online... But now I know the direction, so thanks a lot for it! But, unfortunately, I must admit that I maybe could not use the script right away because the team names ('ABC', 'DEF', ...) were only examples, and in your script they are hard-coded. But I don't know the team names, and additionally they change from season to season... Is it possible to have this script more universal? But you don't need to spend too much work on this. It's really more than I expected, and I don't want to make too much work for you! |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-25 : 17:33:06
|
| The table containing the team names is intended to only be an example (I copied it from your original post). If you will replace the example table with your actual data in the same format as the example table the script will work and pick up your new team names. |
 |
|
|
Heinz23
Yak Posting Veteran
84 Posts |
Posted - 2008-01-25 : 17:43:31
|
| Hi jdaman,yes, that's clear, but when I place the actual team names there I could not use this SP for historical data, right? So if I have 10 seasons (with some different teams) I need to have 10 SPs? |
 |
|
|
jdaman
Constraint Violating Yak Guru
354 Posts |
Posted - 2008-01-25 : 18:32:06
|
No, each season is grouped together. It doesnt matter how many seasons you add or how many teams play a season.For example, your league decided for season 3 to add 4 more teams and expand their season out to 8 games. The script will work just fine as it is using the following data set that represents the above scenario:DECLARE @SeasonData TABLE ( Season INT, MatchRound INT, Team1 CHAR(15), Team2 CHAR(15), GoalsTeam1 INT, GoalsTeam2 INT )INSERT @SeasonData ( Season, MatchRound, Team1, Team2, GoalsTeam1, GoalsTeam2 )SELECT 1, 1, 'ABC', 'DEF', 3, 2 UNIONSELECT 1, 1, 'GHI', 'JKL', 1, 1 UNIONSELECT 1, 2, 'DEF', 'GHI', 1, 0 UNIONSELECT 1, 2, 'JKL', 'ABC', 0, 2 UNIONSELECT 1, 3, 'ABC', 'GHI', 2, 2 UNIONSELECT 1, 3, 'DEF', 'JKL', 0, 0 UNIONSELECT 2, 1, 'ABC', 'DEF', 1, 2 UNIONSELECT 2, 1, 'GHI', 'JKL', 3, 1 UNIONSELECT 2, 2, 'DEF', 'GHI', 1, 1 UNIONSELECT 2, 2, 'JKL', 'ABC', 0, 2 UNIONSELECT 2, 3, 'ABC', 'GHI', 2, 4 UNIONSELECT 2, 3, 'DEF', 'JKL', 0, 1 UNIONSELECT 3, 1, 'ABC', 'Falcons', 5, 6 UNIONSELECT 3, 1, 'DEF', 'Gulls', 2, 2 UNIONSELECT 3, 1, 'GHI', 'Sparrows', 4, 7 UNIONSELECT 3, 1, 'JKL', 'Hawks', 3, 1 UNIONSELECT 3, 2, 'DEF', 'ABC', 4, 5 UNIONSELECT 3, 2, 'GHI', 'Falcons', 2, 1 UNIONSELECT 3, 2, 'JKL', 'Gulls', 8, 7 UNIONSELECT 3, 2, 'Hawks', 'Sparrows', 3, 2 UNIONSELECT 3, 3, 'GHI', 'DEF', 3, 2 UNIONSELECT 3, 3, 'JKL', 'ABC', 6, 1 UNIONSELECT 3, 3, 'Hawks', 'Falcons', 4, 4 UNIONSELECT 3, 3, 'Sparrows', 'Gulls', 2, 6 UNIONSELECT 3, 4, 'JKL', 'GHI', 1, 0 UNIONSELECT 3, 4, 'Hawks', 'DEF', 2, 4 UNIONSELECT 3, 4, 'Sparrows', 'ABC', 5, 1 UNIONSELECT 3, 4, 'Gulls', 'Falcons', 0, 0 UNIONSELECT 3, 5, 'Hawks', 'JKL', 3, 0 UNIONSELECT 3, 5, 'Sparrows', 'GHI', 4, 2 UNIONSELECT 3, 5, 'Gulls', 'DEF', 6, 1 UNIONSELECT 3, 5, 'Falcons', 'ABC', 1, 2 UNIONSELECT 3, 6, 'Sparrows', 'Hawks', 0, 3 UNIONSELECT 3, 6, 'Gulls', 'JKL', 1, 1 UNIONSELECT 3, 6, 'Falcons', 'GHI', 5, 1 UNIONSELECT 3, 6, 'ABC', 'DEF', 2, 2 UNIONSELECT 3, 7, 'Gulls', 'Sparrows', 4, 6 UNIONSELECT 3, 7, 'Falcons', 'Hawks', 3, 2 UNIONSELECT 3, 7, 'ABC', 'JKL', 3, 3 UNIONSELECT 3, 7, 'DEF', 'GHI', 0, 5 UNIONSELECT 3, 8, 'Falcons', 'Gulls', 2, 1 UNIONSELECT 3, 8, 'ABC', 'Sparrows', 1, 1 UNIONSELECT 3, 8, 'DEF', 'Hawks', 4, 2 UNIONSELECT 3, 8, 'GHI', 'JKL', 3, 1 |
 |
|
|
Heinz23
Yak Posting Veteran
84 Posts |
Posted - 2008-01-25 : 19:06:33
|
| Ah ok, I see. Thanks jdaman, I'll try that! |
 |
|
|
|
|
|