0

2 つの一時テーブルを内部結合しようとして
います これが実行できることはわかっていますが、以前に実行したことがありますが、その方法を完全に忘れていました

アドバイスしてください
以下は実行しようとするクエリです

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

これは失敗し
ます I think I am not allowed to use tmp1 in the subquery that create tmp2

メッセージ 4104、レベル 16、状態 1、行 17 マルチパート識別子 "tmp1.IdPlayer" をバインドできませんでした。

4

4 に答える 4

3

2 つの一時テーブルを結合しようとしているのではなく、2 つの派生テーブルを結合しようとしています。

1 つの派生テーブルの内部データは、SELECT 句内にない限り、外部からアクセスできません。

次のことを試してください。

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 GROUP BY IdWinner) as tmp2 
                on tmp2.IdWinner = tmp1.IdPlayer
于 2009-12-18T16:02:46.887 に答える
2
select
    1 as ClassificationType,
    tmp1.IdGame,
    tmp1.IdPlayer,
    sum(tmp1.Score) as Score,
    sum(tmp1.Points) as Points,
    sum(tmp1.OpponentScore) as OpponentScore,
    count(tmp1.ID) as MatchesCount,
    count(distinct tmp1.IdCompetition) as CompetitionsCount,
    count(tmp2.IdWinner) as cnt
from 
    tblMatches tmp1
    inner join
    tblCompetitions tmp2
        on tmp2.IdWinner = tmp1.IdPlayer
group by
    tmp1.IdPlayer,
    tmp1.IdGame
于 2009-12-18T16:14:41.507 に答える
1

の where 句はtmp2、結合条件を複製します。

inner join (select IdWinner, count(IdWinner) as cnt 
            from tblCompetitions 
            where IdWinner = tmp1.IdPlayer) as tmp2 
on         tmp2.IdWinner = tmp1.IdPlayer

where条項を削除するだけです。さらに、Astander が削除された投稿で指摘したように、2 番目のクエリにgroup byも次のものが必要です。

inner join (select IdWinner, count(IdWinner) as cnt 
            from tblCompetitions
            group by IdWinner) as tmp2 
on         tmp2.IdWinner = tmp1.IdPlayer

サブクエリから外側のクエリを参照できない理由は、結合の右側の部分が結合の左側の部分に依存するようになるためです。

于 2009-12-18T16:02:49.533 に答える
1

実際には、2 番目のサブクエリは必要ありません。これはどうですか?

SELECT tmp1.ClassificationType, tmp1.IdGame, tmp1.IdPlayer,
       COUNT(tblCompletions.IdWinner) as 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 tblCompletions ON tmp1.IdPlayer = tblCompletions.IdWinner
GROUP BY tmp1.ClassificationType, tmp1.IdGame, tmp1.IdPlayer
于 2009-12-18T16:06:23.307 に答える