updating a table using the max date and values from another table using sql in ms-access -
i have following scenario: want update tableb
using rating tablea
. challenge is: ratings change randomly , when updating tableb's records, have take in account date of match , if rating changed let's on tuesday , game on monday before, want rating previous rating not latest one.
#table a: contains rating of players, changes randomly @ date depending #on drop of form players pid| rating | datechange | 1 | 2 | 10-may-2014 | 1 | 4 | 20-may-2015 | 1 | 20 | 1-june-2015 | 2 | 4 | 1-april-2014| 3 | 4 | 5-april-2014| 2 | 3 | 3-may-2015 | #table b: contains match sheets. every player has different match sheet #and plays different dates. msid | pid | matchdate | win | rating | 1 | 2 | 10-may-2014 | no | 0 | 2 | 1 | 15-may-2015 | yes | 0 | 3 | 3 | 10-apr-2014 | no | 0 | 4 | 1 | 21-apr-2015 | yes | 0 | 5 | 1 | 3-june-2015 | yes | 0 | 6 | 2 | 5-may-2015 | no | 0 | #i trying achieve running ms-access query: want #every players rating @ time match played not current #rating. msid | pid | matchdate | rating | 1 | 2 | 10-may-2014 | 4 | 2 | 1 | 15-may-2015 | 2 | 3 | 3 | 10-apr-2014 | 4 | 4 | 1 | 21-apr-2015 | 2 | 5 | 1 | 3-june-2015 | 20 | 6 | 2 | 5-may-2015 | 3 |
i tried following code:
update [b-table] wdev set wdev.rating = ( select b.msid, b.pid, b.matchdate, a.rating rating [b-table] b inner join [a-table] on b.pid = a.pid inner join ( select max(y.datechange) mdc, y.pid, z.matchdate [b-table] z inner join [a-table] y on z.pid = y.pid , y.datechange <= z.matchdate group y.pid, z.matchdate) c on c.mdc = a.datechange , a.pid = c.pid , b.matchdate = c.matchdate) , b.msid = wdev.msid
sqlfiddle database schema
in summary: want rating corresponding max date change on or before date of match.
i think want correlated subquery:
update [b-table] b set rating = (select top 1 rating [a-table] a.pid = b.pid , a.datechange <= b.matchdate order a.datechange desc ) ;
note: because of way ms access processes top
, subquery return multiple rows in event of tie. normal solution include additional key value in order by
, prevent ties. however, there not seem unique key in "a" table.
Comments
Post a Comment