Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图计算 A 和 B 曾经参加过的比赛的数量,数据集如下所示:
所以 team1 和 team 29 的比赛次数是 2,因为它们曾经是 HomeTeam 和 AwayTeam,但是,使用我的查询我只能算作一次:
SELECT HomeTeamID, AwayTeamID, Count(*) AS num_matches FROM games GROUP BY HomeTeamID, AwayTeamID
我知道我的问题在哪里,但不知道如何解决。
将团队放在一个共同的顺序中,这样分组就不会对他们是在家还是在外地敏感。
SELECT GREATEST(HomeTeamID, AwayTeamID) AS team1, LEAST(HomeTeamID, AwayTeamID) AS team2, COUNT(*) as num_matches FROM games GROUP BY team1, team2