问题
I'm an ameture using SQL server 2012 and need help with an update query.
I have a SQL table showing results. Every result is unique through the date of the event, the ContestID and the rank of the result (1st 2nd 3rd etc.)
Every result has allocated points depending on how well they did in the event.
Last place, 4th, 3rd, 2nd and 1st have the same points for each event but points between last and 4th are calculated depending on how many results there are.
E.g. 7 RESULTS:
OLD
1st - 150pts
2nd - 125pts
3rd - 100pts
-----------------
4th - 95pts
5th - 90pts
6th - 85pts
7th(Last) - 80pts
NEW
1st - 300pts
2nd - 250pts
3rd - 225pts
4th - 200pts
--------------------
5th - 166.666pts
6th - 133.333pts
7th(Last) - 100pts
This points system is update from an old system and needs to be applied. Can anyone help with the query to calculate the results which are 5th - last. Thanks
回答1:
DROP TABLE #Tmp
CREATE TABLE #Tmp (Place int , Value decimal(22,6))
INSERT INTO #Tmp VALUES(1,300)
INSERT INTO #Tmp VALUES(2,250)
INSERT INTO #Tmp VALUES(3,225)
INSERT INTO #Tmp VALUES(4,200)
Declare @i int
Declare @int int
Declare @ValueToDevide decimal(22,6)
set @ValueToDevide = 100.000000/(10-4)
set @i = 5
set @int = 1
while @i <= 10 begin
INSERT INTO #Tmp VALUES(@i,200-(@ValueToDevide*@int))
set @i = @i + 1
set @int = @int + 1
end
SELECT * FROM #Tmp
ORDER BY Place
I solve your problem only becouse it was interesting, but this was clearly not a SQL Problem, more of a math one.
It almost gets the result you want, a bit more precise. You can figure out what to do next.
回答2:
It was a math problem after all and i just needed an appropriate algorithm.
UPDATE RESULTS
/*
The old system started on 80, the new starts on 100 (-80)&(+100).
The difference between Last and 3rd/4th is now 100 instead of 20 (*5)
*/
SET Points = ((Points - 80)*5)+100
/*Select the appropriate data needed to edit*/
WHERE Position > 4
AND ContestID = 1
OR ContestID = 2
OR ContestID = 3
The top 4 have the same amount of points every time so a separate update can be done for the results below.
来源:https://stackoverflow.com/questions/27295969/update-every-field-depending-on-the-same-and-other-fields