0

我正在使用 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.

我相信我收到了这个错误,因为我插入了变量方程MB我原来的帖子。所以我的问题仍然存在。我将这些方程式放在我的 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)

4

1 回答 1

1

为了在您的 SQL、IE 中返回非聚合字段和聚合函数Avg(G.Growth),您必须提供一个GROUP BY.

通过查看您的代码,我知道这Calculation不是聚合,因为您对字段和表名使用相同的变量。

如果您需要进一步的帮助,您需要提供生成的 SQL 以供检查,我们不知道您的变量中有什么。IE,Debug.Writeline(cmd3.CommandText)

于 2013-10-22T18:54:19.367 回答