我有 Light Athletic 比赛结果的表格。玩家获得前三名的积分。我需要列出他们的点的田径运动员名称。我有这个:
SELECT NAME, SUM(many) as sum FROM
(
(SELECT NAME, count(*) * (SELECT points from "points for place" where place = 1) as many
FROM RESULTS R1 WHERE
(SELECT count(*) FROM RESULTS R2 WHERE
R1.result < R2.result
AND R1.DISCIPLINE = R2.DISCIPLINE
AND R1.CITY = R2.CITY) = 0
GROUP BY NAME)
UNION
(SELECT NAME, count(*) * (SELECT points from "points for place" where place = 2) as many
FROM RESULTS R1 WHERE
(SELECT count(*) FROM RESULTS R2 WHERE
R1.result < R2.result
AND R1.DISCIPLINE = R2.DISCIPLINE
AND R1.CITY = R2.CITY) = 1
GROUP BY NAME)
UNION
(SELECT NAME, count(*) * (SELECT points from "points for place" where place = 3) as many
FROM RESULTS R1 WHERE
(SELECT count(*) FROM RESULTS R2 WHERE
R1.result < R2.result
AND R1.DISCIPLINE = R2.DISCIPLINE
AND R1.CITY = R2.CITY) = 2
GROUP BY NAME)
)
GROUP BY NAME ORDER BY SUM;
我有三倍几乎相同的冗余代码。如果这不取决于地方,我可以使用 View ...