我有一个执行GROUP BY的SQL查询,将包含相同Player_id但不相同Game_id的所有行合并到一起:
SELECT p.Player_id, p.Name, p.Position, SUM(s.Goals) AS goalsb, SUM(s.Assists) AS assistsb, SUM(s.Points) AS pointsb FROM Dim_Player AS p INNER JOIN Fact_Statistics AS s ON s.Player_id = p.Player_id GROUP BY p.Player_id, p.Name, p.Position ORDER BY pointsb DESC, goalsb DESC
我想做的是,每次GROUP BY将一行与另一行合并以创建一个名为“玩过的游戏”的新列时,都植入一个COUNT。例子:
Player_id Game_id goalsb 8470598 465 1 8470598 435 1
它将与上面的SQL查询一起分组为:
Player_id goalsb 8470598 2
但是我想要这个:
Player_id goalsb Games_played 8470598 2 2
如果您有重复Game_id的,并且想要计算不同的值,则可以添加一个
Game_id
COUNT (DISTINCT Game_id)
语句中的子句SELECT。
SELECT