2014-02-28 41 views
0

我有兩個表,GameLog和玩家:兩個和與不同的分組

GameLog:

Player_ID, Team, FGA, Date_Played 

玩家:

Player_ID, FirstName, LastName 

我有兩個疑問是SUM的FGA,然後組他們分別由球隊和球員:

SELECT gl.Tm, SUM(gl.FGA) As TmFGA, gl.Date_Played 
FROM GameLog gl 
GROUP BY gl.Date_Played, gl.Tm; 

其中給出結果:

Team  TmFGA  Date_Played 
GSW  84  2/26/2014 
UTA  74  2/26/2014 

然後:

SELECT p.FirstName, p.LastName, gl.Tm, SUM(gl.FGA) AS FGA, gl.Date_Played 
FROM GameLog AS gl INNER JOIN Players AS p ON p.Player_ID = gl.Player_ID 
GROUP BY gl.Date_Played, gl.Tm, p.LastName, p.FirstName; 

其中給出結果:

FirstName  Lastname  Team  FGA  Date_Played 
Andrew  Bogut  GSW  5  2/26/2014 
Steve   Blake  GSW  6  2/26/2014 
Derrick  Favors  UTA  7  2/26/2014 

顯然,這些都是局部的結果,但你的想法。

最終目標是對FGA進行計算的一個查詢,與團隊的總FGA進行比較,但第一步是將玩家的FGA和他的團隊的FGA都計入相同的查詢。

所以對於現在的第一步是得到這樣的結果:

FirstName  LastName  Team  FGA  TmFGA  Date_Played 
Andrew  Bogut  GSW  5  84  2/26/2014 
Steve   Blake  GSW  6  84  2/26/2014 
Derrick  Favors  UTA  7  74  2/26/2014 

作爲一個方面說明,每當我跑的球員具體的查詢,我得到一個提示,要求對「Query1.FirstName」。當我點擊確定它運行,但任何想法,爲什麼我得到這個提示?

回答

0
SELECT y.FirstName, y.LastName, y.Tm, y.FGA, y.Date_Played,x.TmFGA FROM 
(SELECT p.FirstName as FirstName, p.LastName as LastName, gl.Tm as Tm, SUM(gl.FGA) AS FGA, 
     gl.Date_Played as Date_Played 
FROM GameLog AS gl INNER JOIN Players AS p ON p.Player_ID = gl.Player_ID 
    GROUP BY gl.Date_Played, gl.Tm, p.LastName, p.FirstName)y 
JOIN 
(SELECT Tm, SUM(FGA) As TmFGA, Date_Played 
FROM GameLog 
GROUP BY Date_Played, Tm)x 
ON x.Tm=y.Tm 
+0

這似乎很接近,但是我在最後一個JOIN上收到語法錯誤。我需要在兩個JOIN周圍使用括號嗎? – fullOfQuestions

+0

那麼,錯誤是什麼? – Mihai

+0

查詢表達式中的語法錯誤(缺少操作符)'p.FirstName JOIN(選擇Tm,Sum(FGA)As TmFGA Date_Played FROM GameLog GROUP BY Date_Played,Tm)x ON x.Tm = gl.Tm' – fullOfQuestions