我正在使用 MS Access 运行一些计算。以下是给我带来麻烦的整体代码:
Dim cmd3 As OleDbCommand = New OleDbCommand("INSERT INTO Criteria (Calculation, [Interval], Tier_Juncture_1, Weight) _
SELECT ?, ?, (M * 0) + B _AS Tier_Juncture_1, _
((Avg(C." & Calculation & " * G.Growth) - Avg(C." & Calculation & ") * Avg(G.Growth)) / (StDevP(C." & Calculation & ") * StDevP(G.Growth)) * 10) AS Weight _
FROM (Sum(G.Growth) / Count(*)) - (1 * (Sum(C." & Calculation & ") / Count(*))) AS B, _
(Sum(C." & Calculation & " * G.Growth) - ((Sum(C." & Calculation & ") * Sum(G.Growth)) / Count(*))) / (Sum(C." & Calculation & " * C." & Calculation & ") - ((Sum(C." & Calculation & ") * Sum(C." & Calculation & ") / Count(*)))) AS M, _
" & Calculation & " AS C INNER JOIN " & Growth_Rate & " AS G _
" & On_Statement & "", con)
cmd3.Parameters.AddWithValue("@Calculation", Calculation)
cmd3.Parameters.AddWithValue("@[Interval]", Interval)
cmd3.ExecuteNonQuery()
更具体地说,我的目标是将此公式的结果插入到我Criteria
在字段中命名的表中Tier_Juncture_1
:
(M * 0) + B _AS Tier_Juncture_1
如您所见,我在这个等式中有两个变量,M
并且B
。以下是每个公式:
(Sum(G.Growth) / Count(*)) - (1 * (Sum(C." & Calculation & ") / Count(*))) AS B
(Sum(C." & Calculation & " * G.Growth) - ((Sum(C." & Calculation & ") * Sum(G.Growth)) / Count(*))) / (Sum(C." & Calculation & " * C." & Calculation & ") - ((Sum(C." & Calculation & ") * Sum(C." & Calculation & ") / Count(*)))) AS M
我的问题是,在我的 SQL 语句中,我应该将两个变量的公式放在哪里,以便我的原始公式Tier_Juncture_1
可以使用这些变量?正如您在上面看到的,我试图将它们放在我的FROM
子句中,但我收到了这个错误:Syntax error in FROM clause.
变量本身的公式应该没有任何问题。我已经独立测试了它们,它们都返回正确的值而没有错误。我只需要知道将它们放在我的 SQL 语句中的什么位置。
更新
根据下面的建议,我已经从我的 SQL 语句中删除了参数。现在看起来像这样:
Dim cmd3 As OleDbCommand = New OleDbCommand("INSERT INTO Criteria (Calculation, [Interval], Tier_Juncture_1, Weight) _
SELECT " & Calculation & ", " & Interval & ", (M * 0) + B AS Tier_Juncture_1, _
((Avg(C." & Calculation & " * G.Growth) - Avg(C." & Calculation & ") * Avg(G.Growth)) / (StDevP(C." & Calculation & ") * StDevP(G.Growth)) * 10) AS Weight _
FROM (Sum(G.Growth) / Count(*)) - (1 * (Sum(C." & Calculation & ") / Count(*))) AS B, _
(Sum(C." & Calculation & " * G.Growth) - ((Sum(C." & Calculation & ") * Sum(G.Growth)) / Count(*))) / (Sum(C." & Calculation & " * C." & Calculation & ") - ((Sum(C." & Calculation & ") * Sum(C." & Calculation & ") / Count(*)))) AS M, _
" & Calculation & " AS C INNER JOIN " & Growth_Rate & " AS G _
" & On_Statement & "", con)
cmd3.ExecuteNonQuery()
但是当我运行它时,我收到以下错误:Syntax error in FROM Clause.
我相信我收到了这个错误,因为我插入了变量方程M
和B
我原来的帖子。所以我的问题仍然存在。我将这些方程式放在我的 SQL 语句中的什么位置。
第二次更新
下面是返回的 SQL 语句Debug.Writeline(cmd3.CommandText)
:
INSERT INTO Criteria (Calculation, [Interval], Tier_Juncture_1, Weight) SELECT Current_Ratio, MRQ, (M * 0) + B AS Tier_Juncture_1, ((Avg(C.Current_Ratio * G.Growth) - Avg(C.Current_Ratio) * Avg(G.Growth)) / (StDevP(C.Current_Ratio) * StDevP(G.Growth)) * 10) AS Weight FROM (Sum(G.Growth) / Count(*)) - (1 * (Sum(C.Current_Ratio) / Count(*))) AS B, (Sum(C.Current_Ratio * G.Growth) - ((Sum(C.Current_Ratio) * Sum(G.Growth)) / Count(*))) / (Sum(C.Current_Ratio * C.Current_Ratio) - ((Sum(C.Current_Ratio) * Sum(C.Current_Ratio) / Count(*)))) AS M, Current_Ratio AS C INNER JOIN Quarterly_Growth_Rates AS G ON (C.Ticker = G.Ticker) AND ((IIF(C.Period = 4, C.Year + 1, C.Year)) = G.Year) AND ((IIF(C.Period = 4, 1, C.Period + 1)) = G.Qtr)