Saya akan menghitung terlebih dahulu nilai teratas dan menyimpannya di tabel sementara sebelum melakukan PEMBARUAN:
SELECT
R1.Date,
R1.ContestID,
(SELECT TOP 1 R2.Position
FROM Results R2
WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID
ORDER BY R2.Position DESC) AS TopPosition
INTO #temp
FROM
(SELECT DISTINCT Date, ContestID FROM Results) R1;
UPDATE Results R
SET R.Points = 100 + ((100/((SELECT TopPosition FROM #temp T
WHERE T.Date = R.Date AND
T.ContestID = R.ContestID)-4) *
((SELECT TopPosition FROM #temp T
WHERE T.Date = R.Date AND
T.ContestID = R.ContestID)-R.Position)))
WHERE R.ContestID > 11
AND R.Position > 4
AND R.Position < (SELECT TopPosition FROM #temp T
WHERE T.Date = R.Date AND T.ContestID = R.ContestID);
Namun, saya tidak yakin bagaimana NumberOfResults
dan NumberOfPlaces
didefinisikan. Anda memilih posisi teratas tetapi tidak pernah menghitung angka apa pun. Mungkin Anda dapat menambahkan ini ke SELECT:
...,
(SELECT COUNT(*)
FROM Results R2
WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID) AS NumberOfResults
INTO ...