我已成功运行此代码一段时间了,现在使用大约15股历史股票价格的简短数据集(例如:http://ichart.yahoo.com/table.csv?s=goog)。我只是将数据量扩大到大约6,000只股票(我DataTable
中的920,570条记录),现在我遇到了一些问题。
以下是代码:
Public Sub Update_Quarterly_Growth_Rates()
ToolStripStatusLabel2.Text = "Updating Quarterly Growth Rates"
con.Open()
Try
Dim cmd As OleDbCommand = New OleDbCommand("CREATE PROC Stock_Price_with_qtr AS SELECT Historical_Stock_Prices.*, Year([Date]) AS [Year], Switch(Month([Date])<4,1,Month([Date])<7,2,Month([Date])<10,3,True,4) AS Qtr FROM Historical_Stock_Prices", con)
cmd.ExecuteNonQuery()
Catch ex As Exception
Finally
Dim cmda As OleDbCommand = New OleDbCommand("DROP PROCEDURE Stock_Price_with_qtr", con)
cmda.ExecuteNonQuery()
Dim cmdb As OleDbCommand = New OleDbCommand("CREATE PROC Stock_Price_with_qtr AS SELECT Historical_Stock_Prices.*, Year([Date]) AS [Year], Switch(Month([Date])<4,1,Month([Date])<7,2,Month([Date])<10,3,True,4) AS Qtr FROM Historical_Stock_Prices", con)
cmdb.ExecuteNonQuery()
End Try
Try
Dim cmd1 As OleDbCommand = New OleDbCommand("CREATE PROC Qtr_Dates AS SELECT Stock_Price_with_qtr.Ticker, Stock_Price_with_qtr.[Year], Stock_Price_with_qtr.Qtr, Min(Stock_Price_with_qtr.[Date]) AS Qtr_Start, Max(Stock_Price_with_qtr.[Date]) AS Qtr_End FROM Stock_Price_with_qtr GROUP BY Stock_Price_with_qtr.Ticker, Stock_Price_with_qtr.[Year], Stock_Price_with_qtr.Qtr", con)
cmd1.ExecuteNonQuery()
Catch ex As Exception
Finally
Dim cmd1a As OleDbCommand = New OleDbCommand("DROP PROCEDURE Qtr_Dates", con)
cmd1a.ExecuteNonQuery()
Dim cmd1b As OleDbCommand = New OleDbCommand("CREATE PROC Qtr_Dates AS SELECT Stock_Price_with_qtr.Ticker, Stock_Price_with_qtr.[Year], Stock_Price_with_qtr.Qtr, Min(Stock_Price_with_qtr.[Date]) AS Qtr_Start, Max(Stock_Price_with_qtr.[Date]) AS Qtr_End FROM Stock_Price_with_qtr GROUP BY Stock_Price_with_qtr.Ticker, Stock_Price_with_qtr.[Year], Stock_Price_with_qtr.Qtr", con)
cmd1b.ExecuteNonQuery()
End Try
Try
Dim cmd2 As OleDbCommand = New OleDbCommand("CREATE PROC Quarterly_Growth_Rates AS SELECT Qtr_Dates.Ticker, Qtr_Dates.[Year], Qtr_Dates.Qtr, (([Close_Prices]![Close]/[Open_Prices]![Open])-1)*100 AS Growth_Rate FROM (Historical_Stock_Prices AS Open_Prices INNER JOIN Qtr_Dates ON (Open_Prices.Ticker = Qtr_Dates.Ticker) AND (Open_Prices.[Date] = 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.[Date])", con)
cmd2.ExecuteNonQuery()
Catch ex As Exception
Finally
Dim cmd2a As OleDbCommand = New OleDbCommand("DROP PROCEDURE Quarterly_Growth_Rates", con)
cmd2a.ExecuteNonQuery()
Dim cmd2b As OleDbCommand = New OleDbCommand("CREATE PROC Quarterly_Growth_Rates AS SELECT Qtr_Dates.Ticker, Qtr_Dates.[Year], Qtr_Dates.Qtr, (([Close_Prices]![Close]/[Open_Prices]![Open])-1)*100 AS Growth_Rate FROM (Historical_Stock_Prices AS Open_Prices INNER JOIN Qtr_Dates ON (Open_Prices.Ticker = Qtr_Dates.Ticker) AND (Open_Prices.[Date] = 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.[Date])", con)
cmd2b.ExecuteNonQuery()
End Try
Close()
ToolStripStatusLabel2.Text = ""
End Sub
Private Sub Load_Quarterly_Growth_Rates()
con.Open()
Dim cmd3 As OleDbCommand = New OleDbCommand("TRANSFORM First(y.Growth_Rate) AS FirstOfGrowth Select y.Ticker FROM Quarterly_Growth_Rates AS y GROUP BY y.Ticker ORDER BY y.Year & '-' & y.Qtr DESC PIVOT y.Year & '-' & y.Qtr", con)
Dim reader As OleDbDataReader = cmd3.ExecuteReader()
Dim da As New DataTable
da.Load(reader)
DataGridView_Quarterly_Growth_Rates.DataSource = da
reader.Close()
con.Close()
End Sub
当我尝试通过将查询加载到DataGridView
来显示查询时,出现Division by zero
错误。我不太确定从哪里开始。我想我想忽略任何受除以零影响的结果,只返回一个空白。
答案 0 :(得分:1)
在您的增长计算中,使用类似
的内容IIf([Open_Prices]![Open] <> 0,
(([Close_Prices]![Close]/[Open_Prices]![Open])-1)*100,
null
)
而不是
(([Close_Prices]![Close]/[Open_Prices]![Open])-1)*100
如果分母为0
,则检查0
并避免执行除法。