某表有如下4个字段
上面是 选手A 、选手B、 A胜局数、 B胜局数的字段
现在要得到 每个选手的 胜场数,胜局数,败局数
只要每条数据(一场比赛)中,该名选手的胜局数大于对方的胜局数既是比赛胜利,胜场数加一。
每名选手在一场比赛里既可能是PlayerA,也可能是PlayerB。
经过尝试得出下面答案,思路比较简单需要先查出两种结果集再并集之后分组得出最终结果
select t.name 姓名, SUM(win) 胜场,SUM(winf) 胜局,SUM(failf) 败局 from(SELECT playerB name,sum(case when recordB>recordA then 1 else 0 end) win, sum(recordB) winf ,sum(recordA) failf from billboard_record GROUP BY playerB UNION ALL SELECT playerA name,sum(case when recordA>recordB then 1 else 0 end) win, sum(recordA) winf ,sum(recordB) failf from billboard_record GROUP BY playerA ) tGROUP BY t.name
查询结果: