I have a table called quotes, it has 3 columns, qDate, qTicker, qClose. this table contains 2 million records. I want to create a new column in this table (5DayReturn) that calculates a value based on a record set. here is an example of the table
qDate, qTicker, qCloseCLOSE, 5DayReturn
5/15/2014, GOOG, 265.25,
5/14/2014, GOOG, 268.75,
5/13/2014, GOOG, 262,50,
5/12/2014, GOOG, 265.00,
5/11/2014, GOOG, 263,90,
5/10/2014, GOOG, 259.00,
i want to loop each row and take the current qClose and the next 5 qClose to calculate a return: (qClose(5)-qClose(0))/qClose(0).
My plan is to loop each row, use the current rows qDate qClose, calculate the value then place it in the row, ill do this for each 2 million rows.
Is this the best way to accomplish this task? Any advise on code or best way to do this?
here is the SQL select query
qDate, qTicker, qCloseCLOSE, 5DayReturn
5/15/2014, GOOG, 265.25,
5/14/2014, GOOG, 268.75,
5/13/2014, GOOG, 262,50,
5/12/2014, GOOG, 265.00,
5/11/2014, GOOG, 263,90,
5/10/2014, GOOG, 259.00,
i want to loop each row and take the current qClose and the next 5 qClose to calculate a return: (qClose(5)-qClose(0))/qClose(0).
My plan is to loop each row, use the current rows qDate qClose, calculate the value then place it in the row, ill do this for each 2 million rows.
Is this the best way to accomplish this task? Any advise on code or best way to do this?
here is the SQL select query
Code:
SELECT TOP 6 qClose, qDate FROM Quotes WHERE qTicker = rs.Fields("qClose") and qDate >= rs.Fields("qDate") ORDER BY qDate;
Code:
Sub CalculateReturn
Dim rs as RecordSet
Dim i as Long
Dim db as Database
Set db = Currentdb
Set rs = db.OpenRecordSet("Quotes")
for i = 0 to rs.recordcount
ticker = rs.fields(qTicker")
currDate = rs.Fields("qDate")
query = SELECT TOP 6 qClose, qDate FROM Quotes WHERE qTicker = rs.Fields("qClose") and qDate >= rs.Fields("qDate") ORDER BY qDate;
' Loop recordset, grab first/last value calculate return assign to variable
'assign return variable to new field..
next i
end sub