I’ve just started using SQL and had been getting by with searching google for quick tips until now, however I’ve been stuck on this one for a while.
I have two tables, Stock_Data has 2 columns, Id and Date. The second table is very large, containing a couple of million rows of data and is called Options_Data. This second table also has an Id column and a Date column along with around 12 extra data columns that I’d like to download.
What I would like to do is
for each row i in Stock_Data
SELECT * FROM Option_Data where Option_Data.Id = Stock_Data.Id(i)
AND WHERE Option_Data.Date BETWEEN Stock_Data.Date-60 days and Stock_data.Date+60 days
end
Essentially; grab a stock ID and it’s unique event date from Stock_Data, head over to Option_Data and take out the data for that stock ID 60 days before and after the event date.
I’ve only ever really coded properly in matlab hence the loop analogy and pseudo code.
I can solve the problem using Id’s only but it involves downloading far more data than is necessary, it’s the unique range of data for each ID that has me in trouble.
1 Answer