您可以尝试像这个小提琴这样的自我加入:
只是为了扩展您可以使用它的其他一些方式,我已将原始小提琴更新为this。此外,我为用户 4 添加了一个额外的数据点,以显示重复项发生了什么。
SELECT c1.*
FROM completedscenarios c1
LEFT JOIN completedscenarios c2
ON c1.user_id = c2.user_id
AND c1.game = c2.game
AND c1.CompletePercentage < c2.completePercentage
WHERE c1.user_id = 1 and c2.user_id is null;
SELECT c1.user_id, c1.game, c1.CompletePercentage
FROM completedscenarios c1
LEFT JOIN completedscenarios c2 ON c1.user_id = c2.user_id
AND c1.game = c2.game
AND c1.CompletePercentage < c2.completePercentage
WHERE c2.user_id is null
ORDER BY c1.user_id, c1.CompletePercentage;
SELECT c1.game, c1.user_id, c1.CompletePercentage
FROM completedscenarios c1
LEFT JOIN completedscenarios c2 ON c1.game = c2.game
AND c1.CompletePercentage < c2.completePercentage
WHERE c2.game is null
ORDER BY c1.game, c1.CompletePercentage;
SELECT c1.user_id, c1.game, c1.CompletePercentage
FROM completedscenarios c1
LEFT JOIN completedscenarios c2 ON c1.user_id = c2.user_id
AND c1.CompletePercentage < c2.completePercentage
WHERE c2.user_id is null
ORDER BY c1.user_id, c1.CompletePercentage;
USER_ID | GAME | COMPLETEPERCENTAGE
------------------------------------
1 | name1 | 90
1 | name4 | 80
1 | name3 | 70
USER_ID | GAME | COMPLETEPERCENTAGE
------------------------------------
1 | name3 | 70
1 | name4 | 80
1 | name1 | 90
2 | name2 | 70
3 | name4 | 100
4 | name1 | 50
4 | name2 | 70
GAME | USER_ID | COMPLETEPERCENTAGE
------------------------------------
name1 | 1 | 90
name2 | 2 | 70
name2 | 4 | 70
name3 | 1 | 70
name4 | 3 | 100
USER_ID | GAME | COMPLETEPERCENTAGE
------------------------------------
1 | name1 | 90
2 | name2 | 70
3 | name4 | 100
4 | name2 | 70