我正在使用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语句中。
第二次更新
以下是Debug.Writeline(cmd3.CommandText)
返回的SQL语句:
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)
答案 0 :(得分:1)
为了在您的SQL Avg(G.Growth)
中返回非聚合字段和聚合函数,您必须使用GROUP BY
。
通过查看您的代码,我知道Calculation
不是聚合,因为您对字段和表名使用相同的变量。
如果您需要进一步的帮助,您需要提供生成的SQL以供检查,我们不知道您的变量中有什么。 IE,Debug.Writeline(cmd3.CommandText)