aq
.from(allGames)
.select(
"game_id",
"team_abbreviation_home",
"team_abbreviation_away",
"game_date",
"wl_home",
"wl_away",
"pts_home",
"pts_away"
)
.fold(["wl_home", "wl_away"], {
as: ["wl_type", "outcome"]
})
.fold(["team_abbreviation_home", "team_abbreviation_away"], {
as: ["team_type", "team"]
})
.fold(["pts_home", "pts_away"], {
as: ["pts_type", "pts"]
})
.filter(
(d) =>
(d.wl_type == "wl_away" &&
d.team_type == "team_abbreviation_away" &&
d.pts_type === "pts_away") ||
(d.wl_type == "wl_home" &&
d.team_type == "team_abbreviation_home" &&
d.pts_type === "pts_home")
)
.derive({
location: (d) =>
d.team_type === "team_abbreviation_home" ? "home" : "away"
})
.relocate(["team", "location"], { after: "game_id" })
.select(aq.not("wl_type", "team_type", "pts_type"))
.view(20)