我不小心跑了两次查询,数据库中的所有点都弄乱了(3000条记录)。
每个结果的前4个都有固定的结果,但计算了最后一个和第五个之间的分数(最后是100分)。
Click here了解更多信息
我基本上需要将此语句转换为SQL:
积分= 100 +((100 /(NumberOfResults - 4))*(NumberOfResults - PositionOfResult))
如何获取select语句以分别引用Select和Update表。这不起作用:
UPDATE Results R1
SET R1.Points = 100 + ((100/((SELECT TOP 1 R2.Position FROM Results R2 WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID ORDER BY R2.Position DESC)-4) * ((SELECT TOP 1 R2.Position FROM Results R2 WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID ORDER BY R2.Position DESC)-R1.Position)))
WHERE R1.ContestID > 11
AND R1.Position > 4
AND R1.Position < (SELECT TOP 1 R2.Position FROM Results R2 WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID ORDER BY R2.Position DESC)
答案 0 :(得分:1)
我会预先计算最高值并在执行UPDATE之前将它们存储在临时表中:
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);
但是,我不确定如何定义NumberOfResults
和NumberOfPlaces
。您正在选择最高位置,但从不计算任何数字。也许你可以将它添加到SELECT:
...,
(SELECT COUNT(*)
FROM Results R2
WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID) AS NumberOfResults
INTO ...
答案 1 :(得分:1)
只需要使用“FROM”
从正确的表中显示更新UPDATE R1
SET R1.Points = 100 + ((100/((SELECT TOP 1 R2.Position FROM Results R2 WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID ORDER BY R2.Position DESC)-4) * ((SELECT TOP 1 R2.Position FROM Results R2 WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID ORDER BY R2.Position DESC)-R1.Position)))
From Results R1
WHERE R1.ContestID > 11
AND R1.Position > 4
AND R1.Position < (SELECT TOP 1 R2.Position FROM Results R2 WHERE R2.Date = R1.Date AND R2.ContestID = R1.ContestID ORDER BY R2.Position DESC)