Skip to content
Advertisement

Calculating returns within SQL query

I have data on stock prices for multiple companies for the last 10 years. I want to be able to query this table to return the annual (calendar year) stock price return for each of these stocks. Note that the same dates for each stock may not exist, so I am trying to dynamically calculate the return using the earliest and latest available date for each stock.

My table looks like this:

I am looking to grab the earliest and latest stock price for each stock, as follows:

And finally, I am trying to calculate the return (End of year price / Start of year price – 1)

What would be the most efficient way to achieve this result (as I will be running this on over 1000 stocks in a 10 year period, which is likely to get computationally intensive)?

Advertisement

Answer

It shouldn’t be too bad. I have built this query based on your sample (plus a single row for 2017):

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement