我在 Snowflake 中有一份上个 NBA 赛季每场比赛的球员统计数据报告表。我正在尝试确定一支球队的胜/负记录,以下是相关列,其中球队代表球员当前所在的球队。 (本样本数据缩写为每场比赛5条记录)
游戏_ID | 玩家 | 团队 | 首页 | HOME_FINAL | 离开 | 客场决赛 | GAME_TIME |
---|---|---|---|---|---|---|---|
1 | 杰伦·布伦森 | NYK | NYK | 112 | 分钟 | 106 | 2024-01-01T15:00:00.000000Z |
1 | 朱利叶斯·兰德尔 | NYK | NYK | 112 | 分钟 | 106 | 2024-01-01T15:00:00.000000Z |
1 | 乔什·哈特 | NYK | NYK | 112 | 分钟 | 106 | 2024-01-01T15:00:00.000000Z |
1 | 唐特·迪文森佐 | NYK | NYK | 112 | 分钟 | 106 | 2024-01-01T15:00:00.000000Z |
1 | OG 阿奴诺比 | NYK | NYK | 112 | 分钟 | 106 | 2024-01-01T15:00:00.000000Z |
2 | 杰伦·布伦森 | NYK | NYK | 116 | 气 | 100 | 2024-01-03T20:30:00.000000Z |
2 | 朱利叶斯·兰德尔 | NYK | NYK | 116 | 气 | 100 | 2024-01-03T20:30:00.000000Z |
2 | 乔什·哈特 | NYK | NYK | 116 | 气 | 100 | 2024-01-03T20:30:00.000000Z |
2 | 唐特·迪文森佐 | NYK | NYK | 116 | 气 | 100 | 2024-01-03T20:30:00.000000Z |
2 | OG 阿奴诺比 | NYK | NYK | 116 | 气 | 100 | 2024-01-03T20:30:00.000000Z |
给出主队、客队的最终比分;以及主队和客队的标识符我想按月/赛季/等查找球队的胜/负记录。看起来很简单,但我认为按照我目前的逻辑,它将每场比赛中的每个球员记录总结为赢/输(因此,如果 NYK 输了并且有 12 名球员参加比赛,我的逻辑会在我需要时将其视为一场比赛的 12 场失利将其视为整场比赛的一场失败)。我也不能 100% 确信我的案例陈述设置正确。我可以改变什么以获得正确的输出?
SELECT
team,
AWAY_WINS + HOME_WINS as WINS,
AWAY_LOSSES + HOME_LOSSES as LOSS
FROM (
SELECT
game_id,
team,
SUM(CASE WHEN away = team AND away_final > home_final THEN 1 ELSE 0 END) AS AWAY_WINS,
SUM(CASE WHEN home = team AND away_final < home_final THEN 1 ELSE 0 END) AS HOME_WINS,
SUM(CASE WHEN away = team AND away_final < home_final THEN 1 ELSE 0 END) AS AWAY_LOSSES,
SUM(CASE WHEN home = team AND away_final > home_final THEN 1 ELSE 0 END) AS HOME_LOSSES
FROM PLAYER_REPORTING_TABLE t1
WHERE MONTH(TO_DATE(t1.GAME_TIME)) = 1 AND YEAR(TO_DATE(t1.GAME_TIME)) = 2024 AND team = 'NYK'
GROUP BY team, game_id
)
GROUP BY team, wins, loss;
select team_id,
sum(case when pts > opponent_pts then 1 else 0 end) as wins,
sum(case when pts < opponent_pts then 1 else 0 end) as losses
from (
select distinct game_id, team,
case when home = team
then home_final else away_final end as pts,
case when home = team
then away_final else home_final end as opponent_pts
from PLAYER_REPORTING_TABLE
where ...
) as scores
group by team_id;
该内部查询将为每支球队每场比赛提供一行。之后就很容易总结了。