我会清楚地解释一下: 我按位置在桌子上有曲棍球统计数据
玩家12:
StatsID / PlayerID / Position / gamesPlayed / points / Season
1 / 12 / RW / 40 / 32 / 2013-2014
2 / 12 / Def / 1 / 1 / 2013-2014
所以我想要一个像这样的查询
Select PlayerID , Season , sum(gamesPlayed) as GP, sum(points) as pts , ???? from
MyPointTable GROUP BY PlayerID, SEASON
我想替换我的????这将使我发挥最重要的位置,所以他像RW一样玩40场比赛。我不能指望,我无法总结。
SQL结果应该给出
ID , Season , GP , pts, MostPosition
12 , 2013-2014 , 41 , 33 , RW
由于
答案 0 :(得分:2)
这适用于PostgreSQL:
SELECT
PlayerID AS ID,
Season,
subQuery.sqTotalGames AS GP,
subQuery.sqTotalPoints AS pts,
position AS MostPosition
FROM
MyPointTable
INNER JOIN
(
SELECT
PlayerID as sqPlayerID,
MAX(gamesPlayed) as sqMaxGames,
SUM(gamesPlayed) as sqTotalGames,
SUM(points) AS sqTotalPoints
FROM
MyPointTable
GROUP by playerID
) subQuery
ON subQuery.sqMaxGames = gamesPlayed
AND subQuery.sqPlayerID = PlayerID;
答案 1 :(得分:0)
它是Sql Server你可以先使用SQL Rank来获得最多的播放然后进行分组