I am trying to inner join 2 temp tables
I know this can be done, I have done this before but i completely forgot how to do it
Please advise me
Below is the query that I try to execute
select tmp1.*, tmp2.cnt from
(
select
1 as ClassificationType,
tblMatches.IdGame,
tblMatches.IdPlayer,
sum(Score) as Score, sum(Points) as Points, sum(OpponentScore) as OpponentScore,
count(ID) as MatchesCount, count(distinct IdCompetition) as CompetitionsCount
from
tblMatches
group by IdPlayer, IdGame
) as tmp1
inner join (select IdWinner, count(IdWinner) as cnt from tblCompetitions where IdWinner = tmp1.IdPlayer) as tmp2
on tmp2.IdWinner = tmp1.IdPlayer
This will fail with
I think I am not allowed to use tmp1 in the subquery that create tmp2
Msg 4104, Level 16, State 1, Line 17
The multi-part identifier
“tmp1.IdPlayer” could not be bound.
You are not trying to join two temp tables, but two derived tables.
You cannot access the inner data of one derived table in outside of it unless it’s in the SELECT clause.
Try the following: