sql server - Messed up SQL data - Select within update statement -
i accidentally ran query twice , points in database have messed (3000 records).
top 4 each result has fixed results points between last , 5th calculated (last 100pts).
click here more info
i need statement converting sql:
points = 100 + ((100 / (numberofresults - 4)) * (numberofresults - positionofresult))
how can select statement refer select , update table separately. doesn't work:
update results r1 set r1.points = 100 + ((100/((select top 1 r2.position results r2 r2.date = r1.date , r2.contestid = r1.contestid order r2.position desc)-4) * ((select top 1 r2.position results r2 r2.date = r1.date , r2.contestid = r1.contestid order r2.position desc)-r1.position))) r1.contestid > 11 , r1.position > 4 , r1.position < (select top 1 r2.position results r2 r2.date = r1.date , r2.contestid = r1.contestid order r2.position desc)
i pre-calculate top values , store them in temporary table before performing update:
select r1.date, r1.contestid, (select top 1 r2.position results r2 r2.date = r1.date , r2.contestid = r1.contestid order r2.position desc) topposition #temp (select distinct date, contestid results) r1; update results r set r.points = 100 + ((100/((select topposition #temp t t.date = r.date , t.contestid = r.contestid)-4) * ((select topposition #temp t t.date = r.date , t.contestid = r.contestid)-r.position))) r.contestid > 11 , r.position > 4 , r.position < (select topposition #temp t t.date = r.date , t.contestid = r.contestid);
however, not sure how numberofresults
, numberofplaces
defined. selecting top position never calculating numbers. maybe can add select:
..., (select count(*) results r2 r2.date = r1.date , r2.contestid = r1.contestid) numberofresults ...
Comments
Post a Comment