2016-03-19 5 views
-2

は簡単なクエリのようですが、私はそれを行うことができません、助けてください。彼らは私にいくつかの制限を与えました:複数のサブクエリと一時テーブルを使用しないで、CTEと外側を適用します。スポーツ統計のTSQLクエリできません

And this is the desired result
テーブルは、ゲーム、チーム、プレーヤーです。

This is the DB Schemaが、私はそれをしているチームの1のために言わせているため、各チームの最も大切なプレーヤーがいかにフィルタリングすることはできませんよ: プレーヤー1が2回MVPを獲得し、 プレイヤー2ウォン5回、 プレイヤー3でMVPを1回賭ける

これらをすべて見ると、すべてのプレーヤーの中で最も優勝したプレーヤー2が必要です。

私がこれまで行っているクエリです:

SELECT 
     q.Name, 
     q.Stadium, 
     q.Logo, 
     COUNT(q.Played) as Played, 
     SUM(q.PlayedAtHome) as [Played Home], 
     SUM(q.PlayedAway) as [Played Away], 
     SUM(q.Won) as Won, 
     SUM(q.Lost) as Lost, 
     MAX(q.BigestWon) as BigestWon, 
     q.MVP, 
     Max(mvpXtimes) mvcXtimes 
FROM 
(
SELECT homeTeam.Name, 
     homeTeam.Stadium, 
     homeTeam.Logo, 
     1 as Played, 
     1 as PlayedAtHome, 
     0 as PlayedAway, 
     mvp.Name as MVP, 
     COUNT(mvp.Name) as mvpXtimes, 
     CASE 
      WHEN (g.HomeScore > g.AwayScore) THEN 1 
      ELSE 0 
     END as Won, 
     CASE 
      WHEN (g.HomeScore < g.AwayScore) THEN 1 
      ELSE 0 
     END as Lost, 
     CASE 
      WHEN (g.HomeScore > g.AwayScore) THEN g.HomeScore 
      ELSE 0 
     END as BigestWon, 
     CASE 
      WHEN (g.HomeScore < g.AwayScore) THEN g.HomeScore 
      ELSE 0 
     END as LostScoreLoser 
FROM dbo.Games as g 
     -- Home team 
     INNER JOIN dbo.Teams homeTeam 
     ON g.HomeTeamID = homeTeam.TeamID 
     -- Mvp of the game 
     INNER JOIN dbo.Players as mvp 
     ON g.MVPPlayerID = mvp.PlayerID 
WHERE g.HomeTeamID = 8 
group by homeTeam.Name, 
     homeTeam.Stadium, 
     homeTeam.Logo, 
     mvp.Name, 
     CASE 
      WHEN (g.HomeScore > g.AwayScore) THEN 1 
      ELSE 0 
     END, 
     CASE 
      WHEN (g.HomeScore < g.AwayScore) THEN 1 
      ELSE 0 
     END, 
     CASE 
      WHEN (g.HomeScore > g.AwayScore) THEN g.HomeScore 
      ELSE 0 
     END, 
     CASE 
      WHEN (g.HomeScore < g.AwayScore) THEN g.HomeScore 
      ELSE 0 
     END 
UNION ALL 

SELECT awayTeam.Name, 
     awayTeam.Stadium, 
     awayTeam.Logo, 
     1 as Played, 
     0 as PlayedAtHome, 
     1 as PlayedAway, 
     mvp.Name as MVP, 
     COUNT(mvp.Name) as mvpXtimes, 
     CASE 
      WHEN (g.AwayScore > g.HomeScore) THEN 1 
      ELSE 0 
     END as Won, 
     CASE 
      WHEN (g.AwayScore < g.HomeScore) THEN 1 
      ELSE 0 
     END as Lost, 
     CASE 
      WHEN (g.AwayScore > g.HomeScore) THEN g.AwayScore 
      ELSE 0 
     END as BigestWon, 
     CASE 
      WHEN (g.AwayScore < g.HomeScore) THEN g.AwayScore 
      ELSE 0 
     END as LostScoreLoser 
FROM dbo.Games as g 
     -- Away team 
     INNER JOIN dbo.Teams awayTeam 
     ON g.AwayTeamID = awayTeam.TeamID 
     -- Mvp of the game 
     INNER JOIN dbo.Players as mvp 
     ON g.MVPPlayerID = mvp.PlayerID 
WHERE g.AwayTeamID = 8 
group by awayTeam.Name, 
     awayTeam.Stadium, 
     awayTeam.Logo, 
     mvp.Name, 
     CASE 
      WHEN (g.AwayScore > g.HomeScore) THEN 1 
      ELSE 0 
     END, 
     CASE 
      WHEN (g.AwayScore < g.HomeScore) THEN 1 
      ELSE 0 
     END, 
     CASE 
      WHEN (g.AwayScore > g.HomeScore) THEN g.AwayScore 
      ELSE 0 
     END, 
     CASE 
      WHEN (g.AwayScore < g.HomeScore) THEN g.AwayScore 
      ELSE 0 
     END 
) as q 
GROUP BY q.Name, 
    q.Stadium, 
    q.Logo , 
    q.MVP 
+1

申し訳ありませんが、SOはあなたの宿題のための不正なツールではありません。また、あなたの宿題には「CTEと外部適用を使用する」と書かれていますが、あなたのコードではCTEも外部適用も見られません。 –

+0

あなた自身で何かをやろうとしてください、あなたが本当に立ち往生している時だけ、ここで聞いてください。 –

+0

私は3日間このクエリを試してきました。とにかく私の質問が間違っているとわかりません – MarcosF8

答えて

0

を私は効率的であればわからない、miselftでそれをやったが、動作します:

WITH 
cteGames (TeamID, GameDateTime, Played, PlayedHome, PlayedAway, Won, Lost, BigestWon, BigestLost) 
AS 
    (  
    SELECT 
     q.TeamID, 
     MAX(q.GameDateTime) as GameDateTime, 
     COUNT(q.Played) as Played, 
     SUM(q.PlayedHome) as PlayedHome, 
     SUM(q.PlayedAway) as PlayedAway, 
     SUM(q.Won) as Won, 
     SUM(q.Lost) as Lost, 
     MAX(q.BigestWon) as BigestWon, 
     MAX(q.BigestLost) as BigestLost 
    FROM 
    (
     -- Home team 
     SELECT  
        g.HomeTeamID as TeamID, 
        g.GameDateTime, 
        1 as Played, 
        1 as PlayedHome, 
        0 as PlayedAway, 
        CASE 
         WHEN (g.HomeScore > g.AwayScore) THEN 1 
         ELSE 0 
        END as Won, 
        CASE 
         WHEN (g.HomeScore < g.AwayScore) THEN 1 
         ELSE 0 
        END as Lost, 
        CASE 
         WHEN (g.HomeScore > g.AwayScore) THEN g.HomeScore - g.AwayScore 
         ELSE 0 
        END as BigestWon, 
        CASE 
         WHEN (g.HomeScore < g.AwayScore) THEN g.AwayScore - g.HomeScore 
         ELSE 0 
        END as BigestLost 
     FROM dbo.Games as g 
       INNER JOIN dbo.Teams homeTeam 
       ON g.HomeTeamID = homeTeam.TeamID 

     UNION ALL 

     -- Away team 
     SELECT 
       g.AwayTeamID as TeamID, 
       g.GameDateTime, 
       1 as Played, 
       0 as PlayedHome, 
       1 as PlayedAway, 
       CASE 
        WHEN (g.AwayScore > g.HomeScore) THEN 1 
        ELSE 0 
       END as Won, 
       CASE 
        WHEN (g.AwayScore < g.HomeScore) THEN 1 
        ELSE 0 
       END as Lost, 
       CASE 
        WHEN (g.AwayScore > g.HomeScore) THEN g.AwayScore - g.HomeScore 
        ELSE 0 
       END as BigestWon, 
       CASE 
        WHEN (g.AwayScore < g.HomeScore) THEN g.HomeScore - g.AwayScore 
        ELSE 0 
       END as BigestLost 
     FROM dbo.Games as g 
       INNER JOIN dbo.Teams awayTeam 
       ON g.AwayTeamID = awayTeam.TeamID 
    ) q 
    GROUP BY q.TeamID 

) 

SELECT t.Name, t.Stadium, t.Logo, cte.Played, cte.PlayedHome,     cte.PlayedAway, cte.Won, cte.Lost, bw.Score as BigestWon, bl.Score as BigestLost, cte.GameDateTime, a.Name as MVP 
FROM dbo.Teams as t 
    INNER JOIN cteGames cte 
    ON t.TeamID = cte.TeamID 
CROSS APPLY 
(
    -- MVP part 
    SELECT TOP 1 
     p.Name 
    FROM games as g 
     INNER JOIN dbo.Players as p 
     ON g.MVPPlayerID = p.PlayerID 
     INNER JOIN dbo.Team_Player as tp 
     ON p.PlayerID = tp.PlayerID 
    WHERE tp.TeamID = t.TeamID 
    GROUP BY p.Name 
    ORDER BY COUNT(g.MVPPlayerID) DESC 
) a 
CROSS APPLY 
dbo.fn_GetBigestWin (T.TeamID) as bw 
CROSS APPLY 
dbo.fn_GetBigestLost(T.TeamID) as bl 


ALTER FUNCTION [dbo].[fn_GetBigestWin] 
(
    @teamId INT 
) 
RETURNS TABLE 
AS 
RETURN 
( 
SELECT 
     TOP 1 
     a.GameID, 
     a.Score 
FROM 
     (
       SELECT 
        g.GameID, 
        g.HomeScore - g.AwayScore as ScoreDifference, 
        RTRIM(LTRIM(STR(g.HomeScore))) + '-' + RTRIM(LTRIM(STR(g.AwayScore))) as Score 
       FROM games as g 
       WHERE g.HomeTeamID = @teamId 
        and g.HomeScore > g.AwayScore 

       UNION ALL 

       SELECT 
        g.GameID, 
        g.AwayScore - g.HomeScore as ScoreDifference, 
        RTRIM(LTRIM(STR(g.AwayScore))) + '-' + RTRIM(LTRIM(STR(g.HomeScore))) as Score 
       FROM games as g 
       WHERE g.AwayTeamID = @teamId 
        and g.AwayScore > g.HomeScore 
     ) a 
GROUP BY a.GameID, 
      a.Score 
ORDER BY MAX(a.ScoreDifference) DESC 
) 


ALTER FUNCTION [dbo].[fn_GetBigestLost] 
(
    @teamId INT 
) 
RETURNS TABLE 
AS 
RETURN 
    ( 
    SELECT 
      TOP 1 
      a.GameID, 
      a.Score 
    FROM 
      (
        SELECT 
         g.GameID, 
         g.AwayScore - g.HomeScore as ScoreDifference, 
         RTRIM(LTRIM(STR(g.HomeScore))) + '-' + RTRIM(LTRIM(STR(g.AwayScore))) as Score 
        FROM games as g 
        WHERE g.HomeTeamID = @teamId 
         and g.HomeScore < g.AwayScore 

        UNION ALL 

        SELECT 
         g.GameID, 
         g.HomeScore - g.AwayScore as ScoreDifference, 
         RTRIM(LTRIM(STR(g.AwayScore))) + '-' + RTRIM(LTRIM(STR(g.HomeScore))) as Score 
        FROM games as g 
        WHERE g.AwayTeamID = @teamId 
         and g.AwayScore < g.HomeScore 
      ) a 
    GROUP BY a.GameID, 
       a.Score 
    ORDER BY MAX(a.ScoreDifference) DESC 
    ) 
関連する問題