Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
warberg
Starting Member
3 Posts |
Posted - 2008-11-26 : 04:51:39
|
| I'm trying to work in http://www.sqlteam.com/article/detecting-runs-or-streaks-in-your-data , the output is exactly what I'm trying to achieve.I'm trying to work it around two tables, res and pgamres:kampid (id for each match)hjemmemaal (home score)udemaal (away score)pgram:kampid (id for each match corresponding to res.kampid)dato (date)I find wins:SELECT SUM(CASE WHEN res.hjemmemaal > res.udemaal THEN 1 ELSE 0 END) as WinsFROM pgram AS p INNER JOIN res AS res ON p.kampid = res.kampidNow, challenge 1; I'm trying to work in Jeffs RunGroup:SELECT GameDate, Result, (SELECT COUNT(*) FROM GameResults G WHERE G.Result <> GR.Result AND G.GameDate <= GR.GameDate) as RunGroup FROM GameResults GRcombining my two tabels pgram and res |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-26 : 04:56:01
|
Are you using SQL Server 2005 and later, or SQL Server 2000 or earlier? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-26 : 05:44:49
|
And maybe more important, how can you tell wether or not a team was home team or away team for a game (kamp). E 12°55'05.63"N 56°04'39.26" |
 |
|
|
warberg
Starting Member
3 Posts |
Posted - 2008-11-26 : 06:25:55
|
| I'm using SQL Server 2005pgram:kampid (id for each match corresponding to res.kampid)dato (date)hjemme (id for home team)ude (id for away team)Id for teams corresponding to a team-table |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-26 : 07:20:10
|
[code]-- Prepare sample dataSET NOCOUNT ONDECLARE @GameResults TABLE ( gameID INT, homeScore INT, awayScore INT )INSERT @GameResultsSELECT 1, 2, 1 UNION ALLSELECT 2, 4, 1 UNION ALLSELECT 3, 4, 3DECLARE @Program TABLE ( gameID INT, gameDate DATETIME, homeID INT, awayID INT )INSERT @ProgramSELECT 1, '2008-05-12', 101, 102 UNION ALLSELECT 2, '2008-05-20', 106, 101 UNION ALLSELECT 3, '2008-05-14', 107, 101-- Prepare staging dataDECLARE @Stage TABLE ( teamID INT, gameDate DATETIME, outcome CHAR(3), streak INT, PRIMARY KEY CLUSTERED ( teamID, gameDate ) )INSERT @Stage ( teamID, gameDate, outcome )SELECT t.teamID, t.gameDate, CASE WHEN t.homeScore = t.awayScore THEN 'tie' WHEN t.theRoute = 'homeID' AND t.homeScore > t.awayScore THEN 'win' WHEN t.theRoute = 'homeID' THEN 'los' WHEN t.theRoute = 'awayID' AND t.homeScore < t.awayScore THEN 'win' WHEN t.theRoute = 'awayID' THEN 'los' END AS outcomeFROM ( SELECT p.homeID, gr.homeScore, p.gameDate, p.awayID, gr.awayScore FROM @GameResults AS gr INNER JOIN @Program AS p ON p.gameID = gr.gameID ) AS gUNPIVOT ( teamID FOR theRoute IN (g.homeID, g.awayID) ) AS tORDER BY teamID, gameDate-- Fix the streak numberingDECLARE @teamID INT, @outcome CHAR(3), @streak INTSELECT TOP 1 @teamID = teamID, @outcome = outcome, @streak = 1FROM @StageORDER BY teamID, gameDateUPDATE @StageSET @streak = streak = CASE WHEN teamID = @teamID AND @outcome = outcome THEN @streak ELSE @streak + 1 END, @teamID = teamID, @outcome = outcomeSELECT teamID, outcome, COUNT(*) AS streaks, MIN(gameDate) AS fromDate, MAX(gameDate) AS toDateFROM @StageGROUP BY teamID, outcomeORDER BY teamID, outcome[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
warberg
Starting Member
3 Posts |
Posted - 2008-11-26 : 14:46:00
|
| Thank you very much for your help.In a few weeks I have to convert all my code to mySQL, any idea how this would look?Or links to the differences in general? |
 |
|
|
|
|
|
|
|