I have the following scenario:
GamehasGameUID, GameNamePlayerhasPlayerUID, PlayerNameGamePlayerhasGamePlayerUID, GameUID, PlayerUID, FinishedPosition
I need to return back one row of a join between these three tables for each Game row where the return row contains the Player row with the lowest finished position.
Example,
Game
{some guid}, Game 1
{some guid}, Game 2
{some guid}, Game 3
Player
{some guid}, Player 1
{some guid}, Player 2
GamePlayer
{some guid}, {game 1 guid}, {player 1 guid}, 1
{some guid}, {game 1 guid}, {player 2 guid}, 2
{some guid}, {game 2 guid}, {player 1 guid}, 2
{some guid}, {game 2 guid}, {player 2 guid}, 1
My expected result would be
Result
-------
{game 1 guid}, Game 1, {player 1 guid}, Player 1
{game 2 guid}, Game 2, {player 2 guid}, Player 2
Now my instincts tell me that this should be the query:
select G.GameUID, G.GameName, V.PlayerUID, P.PlayerName
from Game G inner join (
select top(1) GameUID, PlayerUID
from GamePlayer GP
where GP.GameUID = G.GameUID
order by FinishedPosition asc) as V on V.GameUID = G.GameUID
inner join Player P on V.PlayerUID = P.PlayerUID
Now, the problem with this query is that where GP.GameUID = G.GameUID results in a
multi-part identifier “G.GameUID” could not be bound
error.
If I exclude that and rely only on the join criteria, the subquery ALWAYS has the same first row and so it only joins one of the games.
I hope I’ve made this clear enough for someone to offer an answer.
BTW, this is being put in a View. I do not want to write a stored proc to do this unless absolutely necessary.
Whew. I think that does it? Let me know if it fails.