SQL Pivot总列

时间:2018-07-04 07:37:07

标签: sql sql-server tsql pivot

我正在构建NFL Pickem应用程序。有问题的查询应显示联盟中所有球员的列表,以及他们在给定的一周内赢得每场比赛的球队。此示例在第1周进行了硬编码,以使其更简单并专注于我的主要问题。

我很难为“总积分”添加额外的列。该总分列应基于给定一周内每个玩家的Pick.ConfidencePoints列进行计算。

下面的查询以我想要的方式工作,除了“总点数”列。  每当我尝试添加该列时,事情就会一团糟。

查询当前产生的结果如下:

enter image description here

这是当前查询:

SELECT Player, [1],[2],[3]
FROM    

    (SELECT
        Player.Name AS Player,
        Game.Week,
        Team.CityShort,
        Game.ID AS GameId
    FROM Pick
    LEFT JOIN Player ON Pick.PlayerId = Player.Id
    LEFT JOIN Team ON Pick.PickedWinnerTeamId = Team.Id
    LEFT JOIN Game ON Pick.GameId = Game.Id
    WHERE Game.Week = 1
    GROUP BY Player.Name, Game.Week, Team.CityShort, Game.Id) AS SourceData


PIVOT
(
MAX (CityShort)
FOR GameId IN ([1],[2],[3])
) AS PivotTable

0 个答案:

没有答案