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 |
|
AndyC
Yak Posting Veteran
53 Posts |
Posted - 2004-11-08 : 07:57:51
|
| I have a database of golf stats from which I wish to extract results for a given player. The matches may have been played either with a partner in foursomes or as a singles match.My desired endproduct would look something like:-Match 1 Woods and Mickelson lost to Clarke and WestwoodMatch 2 Woods defeated Rileyetc.Stripped to essentials, I have three tables involvedtblPlayers-----------Fields playerID, teamIDWoods,USMickelson,USClarke, EURWestwood,EURCasey,EURtblPlayerMatch--------------Fields playerID,matchIDWoods,1Mickeson,1Clarke, 1Westwood,1Woods,2Casey,2tblResults----------Fields matchID,winner1,EUR2,USwinner will either = teamID or be 'Halved"I have managed to get a list of the players involved in each match butnot arrived at the end-productAny help much appreciatedAndrew Clarkwww.majorleaguecharts.com |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-11-08 : 09:28:35
|
| Depending on your business needs, I see some potential issues with your current schema. Let me give an example, which may or may not occur in your domain model:2 US, or EUR players are playing against each other (ie. Woods vs Michelson) how would you indicate the winner? |
 |
|
|
AndyC
Yak Posting Veteran
53 Posts |
Posted - 2004-11-08 : 10:22:47
|
| Sorry I should have made this clearerIt is a team competition (actually the Ryder Cup in this example) so it is always US v EUR playersand a player can only ever be on one team even in different yearsAndrew Clarkwww.majorleaguecharts.com |
 |
|
|
VIG
Yak Posting Veteran
86 Posts |
Posted - 2004-11-08 : 13:58:32
|
| [code]declare @Players table(PlayerID varchar(20),teamID varchar(20))declare @PlayerMatch table(PlayerID varchar(20),matchID int)declare @MatchWinner table(matchID int,winner varchar(20))insert @Playersselect 'Woods','US' unionselect 'Mickelson','US' union select 'Clarke', 'EUR' unionselect 'Westwood', 'EUR' unionselect 'Casey','EUR'insert @PlayerMatchselect 'Woods',1 unionselect 'Mickelson',1 unionselect 'Clarke', 1 unionselect 'Westwood',1 unionselect 'Woods',2 unionselect 'Casey',2insert @MatchWinner select 1,'EUR' union select 2,'US'select 'Match ' + cast(a.matchid as varchar(10)) ,b.players + ' lost to ' + a.players from (select mw.matchid ,case when min(pm.PlayerID)= max(pm.PlayerID) then min(pm.PlayerID) else min(pm.PlayerID) + '+' + max(pm.PlayerID) end Players ,case when mw.winner=p.teamID then 'w' else 'l' end r from @MatchWinner mw join @PlayerMatch pm on mw.matchid=pm.matchid join @Players p on pm.playerid=p.playerid group by mw.matchid,case when mw.winner=p.teamID then 'w' else 'l' end ) ajoin (select mw.matchid ,case when min(pm.PlayerID)= max(pm.PlayerID) then min(pm.PlayerID) else min(pm.PlayerID) + '+' + max(pm.PlayerID) end Players ,case when mw.winner=p.teamID then 'w' else 'l' end r from @MatchWinner mw join @PlayerMatch pm on mw.matchid=pm.matchid join @Players p on pm.playerid=p.playerid group by mw.matchid,case when mw.winner=p.teamID then 'w' else 'l' end ) bon a.matchid=b.matchid and a.r='w' and b.r='l'[/code] |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-11-08 : 14:24:46
|
| [code]create function dbo.getPlayers ( @matchid int, @teamid varchar(10) )returns varchar(100)asbegin declare @players varchar(100); set @players = '' select @players = @players + p.playerid + ' and ' from tblplayers p join tblplayermatch pm on pm.playerid = p.playerid and pm.matchid = @matchid and p.teamid = @teamid return left(@players,len(@players)-3)endgoselect 'Match ' + convert(varchar(2),d1.matchid) + ' '+ d1.players + d1.status + d2.players as report, d1.matchid, d1.teamid, d1.players, d1.status, d2.teamid, d2.players, d2.statusfrom( select distinct pm.matchid,teamid,winner, dbo.getPlayers(pm.matchid,teamid) players, case when teamid = winner then 'Defeated ' when teamid != winner and winner != 'Halved' then 'Lost to ' else 'Halved ' end as status from tblplayermatch pm join tblplayers p on p.playerid = pm.playerid join tblresults r on r.matchid = pm.matchid) d1join( select distinct pm.matchid,teamid,winner, dbo.getPlayers(pm.matchid,teamid) players, case when teamid = winner then 'Defeated ' when teamid != winner and winner != 'Halved' then 'Lost to ' else 'Halved ' end as status from tblplayermatch pm join tblplayers p on p.playerid = pm.playerid join tblresults r on r.matchid = pm.matchid) d2 on d2.matchid = d1.matchid and d2.teamid <> d1.teamiddrop function dbo.getPlayers [/code] |
 |
|
|
AndyC
Yak Posting Veteran
53 Posts |
Posted - 2004-11-09 : 13:58:44
|
Thanks very much for the suggestions guysehorn, I had a Invalid object name 'rc_MatchPlayer' (your tblplayermatch) when I adapted it to my tables - not sure whyso I adapted VIGS version utilizing the actual database names and data as followsselect 'Match ' + cast(a.matchid as varchar(10)) ,case when a.r='d' then a.players + ' halved with ' +b.players else a.players + ' defeated ' + b.players endfrom (select mw.matchid ,case when min(p.FirstName+ ' '+p.LastName)= max(p.FirstName+ ' '+p.LastName) then min(p.FirstName+ ' '+p.LastName) else min(p.FirstName+ ' '+p.LastName) + ' & ' + max(p.FirstName+ ' '+p.LastName) end Players ,case when mw.winner=p.team then 'w' when mw.winner != p.team and mw.winner != 'Halved' then 'l' else 'd' end r from rc_Matches mw join rc_MatchPlayer pm on mw.matchid=pm.matchid join rc_Players p on p.PlayerID=pm.playerid group by mw.matchid,case when mw.winner=p.team then 'w' when mw.winner != p.team and mw.winner != 'Halved' then 'l' else 'd' end ) ajoin (select mw.matchid ,case when min(p.FirstName+ ' '+p.LastName)= max(p.FirstName+ ' '+p.LastName) then min(p.FirstName+ ' '+p.LastName) else min(p.FirstName+ ' '+p.LastName) + ' & ' + max(p.FirstName+ ' '+p.LastName) end Players ,case when mw.winner=p.team then 'w' when mw.winner != p.team and mw.winner != 'Halved' then 'l' else 'd' end r from rc_Matches mw join rc_MatchPlayer pm on mw.matchid=pm.matchid join rc_Players p on p.PlayerID=pm.playerid group by mw.matchid,case when mw.winner=p.team then 'w' when mw.winner != p.team and mw.winner != 'Halved' then 'l' else 'd' end ) bon (a.matchid=b.matchid and (a.r='d' and b.r='d')) OR (a.matchid=b.matchid and (a.r='w' and b.r='l')) The only problem with this is that if it is a singles match I get a duplication of the playerse.g. Frank Beard & Neil Coles halved with Frank Beard & Neil ColesI also want to filter to get an individual's results only, based on his playerID whichis in both rc_Players and rc_MatchPlayer but have not solved this eitherAndrew Clarkwww.majorleaguecharts.com |
 |
|
|
AndyC
Yak Posting Veteran
53 Posts |
Posted - 2004-11-11 : 17:43:18
|
I have now solved the 'Halved' problem and also am able to filter by year vizselect 'Match ' + cast(a.matchid as varchar(10)) ,case when a.r='d' then a.players + ' halved with ' +b.players else a.players + ' defeated ' + b.players endfrom (select mw.matchid,p.team, case when min(p.FirstName+ ' '+p.LastName)= max(p.FirstName+ ' '+p.LastName) then min(p.FirstName+ ' '+p.LastName) else min(p.FirstName+ ' '+p.LastName) + ' & ' + max(p.FirstName+ ' '+p.LastName) end Players ,case when mw.winner=p.team then 'w' when mw.winner != p.team and mw.winner != 'Halved' then 'l' else 'd' end r from rc_Matches mw join rc_MatchPlayer pm on mw.matchid=pm.matchid join rc_Players p on p.PlayerID=pm.playerid where mw.year='2004' group by mw.matchid,p.team,case when mw.winner=p.team then 'w' when mw.winner != p.team and mw.winner != 'Halved' then 'l' else 'd' end ) ajoin (select mw.matchid,p.team ,case when min(p.FirstName+ ' '+p.LastName)= max(p.FirstName+ ' '+p.LastName) then min(p.FirstName+ ' '+p.LastName) else min(p.FirstName+ ' '+p.LastName) + ' & ' + max(p.FirstName+ ' '+p.LastName) end Players ,case when mw.winner=p.team then 'w' when mw.winner != p.team and mw.winner != 'Halved' then 'l' else 'd' end r from rc_Matches mw join rc_MatchPlayer pm on mw.matchid=pm.matchid join rc_Players p on p.PlayerID=pm.playerid where left(mw.matchID,4)='2004' group by mw.matchid,p.team,case when mw.winner=p.team then 'w' when mw.winner != p.team and mw.winner != 'Halved' then 'l' else 'd' end ) bon (a.matchid=b.matchid and (a.r='d' and b.r='d') and a.team<>b.team and a.team='EUR') OR (a.matchid=b.matchid and (a.r='w' and b.r='l')) but am still failing to filter for the playerIDAny suggestions?Andrew Clarkwww.majorleaguecharts.com |
 |
|
|
|
|
|
|
|