1

I have a table named "Historical_Stock_Prices" in a MS Access database. This table has the columns: Ticker, Date1, Open1, High, Low, Close1, Volume, Adj_Close. The rows consist of the data for each ticker for every business day.

I need to run a query from inside my VB.net program that will return a table in my program that displays the growth rates for each quarter of every year for each ticker symbol listed. So for this example I would need to find the growth rate for GOOG in the 4th quarter of 2012.

To calculate this manually I would need to take the Close Price on the last BUSINESS day of the 4th quarter (12/31/2012) divided by the Open Price of the first BUSINESS day of the 4th quarter (10/1/2012). Then I need to subtract by 1 and multiply by 100 in order to get a percentage.

The actual calculation would look like this: ((707.38/759.05)-1)*100 = -6.807%

The first and last days of each quarter may vary due to weekend days.

I cannot come up with the correct syntax for the SQL statement to create a table of Growth Rates from a table of raw Historical Prices. Can anyone help me with the SQL statment?

4

1 回答 1

1

这是我解决问题的方法:

我首先创建一个名为 [Stock_Price_with_qtr] 的已保存查询访问,它计算每一行的年份和季度:

SELECT 
    Historical_Stock_Prices.*, 
    Year([Date1]) AS Yr, 
    Switch(Month([Date1])<4,1,Month([Date1])<7,2,Month([Date1])<10,3,True,4) AS Qtr
FROM Historical_Stock_Prices

然后,我将在 Access 中创建另一个名为 [Qtr_Dates] 的已保存查询,用于查找每个股票代码和季度的第一个和最后一个工作日:

SELECT 
    Stock_Price_with_qtr.Ticker, 
    Stock_Price_with_qtr.Yr, 
    Stock_Price_with_qtr.Qtr, 
    Min(Stock_Price_with_qtr.Date1) AS Qtr_Start, 
    Max(Stock_Price_with_qtr.Date1) AS Qtr_End
FROM Stock_Price_with_qtr
GROUP BY 
    Stock_Price_with_qtr.Ticker, 
    Stock_Price_with_qtr.Yr, 
    Stock_Price_with_qtr.Qtr

这将允许我在 VB.NET(或 C#,或 Access 本身)中使用以下查询来计算季度增长率:

SELECT 
    Qtr_Dates.Ticker, 
    Qtr_Dates.Yr, 
    Qtr_Dates.Qtr, 
    (([Close_Prices]![Close1]/[Open_Prices]![Open1])-1)*100 AS Qtr_Growth
FROM 
    (
        Historical_Stock_Prices AS Open_Prices 
        INNER JOIN Qtr_Dates 
        ON (Open_Prices.Ticker = Qtr_Dates.Ticker) 
            AND (Open_Prices.Date1 = Qtr_Dates.Qtr_Start)
    ) 
    INNER JOIN 
    Historical_Stock_Prices AS Close_Prices 
    ON (Qtr_Dates.Ticker = Close_Prices.Ticker)
        AND (Qtr_Dates.Qtr_End = Close_Prices.Date1) 
于 2013-09-18T15:37:28.763 回答