player1_id | score1 | score2 | player2_id
-----------+--------+--------+-----------
1 | 1 | 1 | 2
3 | 1 | 1 | 1
11 | 1 | 0 | 20
5 | 1 | 1 | 55
200 | 1 | 2 | 11
17 | 1 | 1 | 7
11 | 1 | 3 | 4
11 | 1 | 1 | 100
20 | 1 | 1 | 2
20 | 2 | 1 | 33
玩家根据得分1和得分2获得“胜利”,“平局”或“失败”结果。我需要找到能够获得“胜利”,“平局”和“失败”结果的球员。在这种情况下,玩家11和20。
我被困在这里,任何帮助都非常感激。
答案 0 :(得分:2)
如果我理解正确,你需要这个:
select p from (
select player1_id as p, case when score1>score2 then 'W' when score1=score2 then 'D' when score1<score2 then 'L' end as res from your_table
union all
select player2_id as p, case when score1>score2 then 'L' when score1=score2 then 'D' when score1<score2 then 'W' end as res from your_table
) t
group by p
having count( distinct res ) = 3
答案 1 :(得分:2)
您需要将所有玩家分成一列,以及您想要的分数或指标:
select p1
from ((select player1_id as p1, player2_id as p2, score1 as s1, score2 as s2
from t
) union all
(select player2_id as p1, player1_id as p2, score2 as s1, score1 as s2
from t
)
) t
group by p1
having sum( (s1 > s2)::int) > 0 and
sum( (s1 = s2)::int) > 0 and
sum( (s1 < s2)::int) > 0;