两个不同分组的SUMS

时间:2014-02-28 14:56:33

标签: sql ms-access group-by

我有两个表,GameLog和Players:

GameLog:

Player_ID, Team, FGA, Date_Played

玩家:

Player_ID, FirstName, LastName

我有两个查询对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

作为旁注,每当我运行Player特定的查询时,我会得到一个提示,询问“Query1.FirstName”。当我只是点击确定它运行,但任何想法为什么我得到这个提示?

1 个答案:

答案 0 :(得分: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