我该如何解决这个查询?
我收到运行时错误 3122:
您试图执行一个不包含指定表达式 count(*)*t2.Daily_Charge_HKD
作为聚合函数一部分的查询
我想在查询中执行以下操作:
I想要按 event_plan_code 对 Opt_In_Customer_Record 中的所有记录进行分组,并计算每个代码的总计数,然后我通过 t1.event_plan_code = t2.event_plan_code 引用 daily_charge 表中的 daily_charge,并将 daily_charge 乘以每个代码的总计数代码的
这是我的代码:
Private Sub btnGenDaily_Click()
Dim filename As String
Dim prefix As String
Dim qryDef As DAO.QueryDef
Dim dbs As DAO.Database
Set dbs = OpenDatabase(CurrentDb.Name)
If IsNull(txtInputPath.value) Then
MsgBox "Please enter a valid input file location."
Else
If FileExists(txtInputPath.value) Then
If IsNull(txtOutputPath3.value) Then
MsgBox "Please enter a valid output file location."
Else
prefix = GetFileNamePrefix(txtInputPath.value)
sql = "select t1.event_plan_code, count(*), count(*)*t2.Daily_Charge_HKD " & _
"from Opt_In_Customer_Record t1 Inner Join Daily_Charge t2 " & _
"On (t1.event_plan_code=t2.event_plan_code and t2.Home_BMO='" & prefix & "') " & _
"group by t1.event_plan_code " & _
"order by t1.event_plan_code "
MsgBox sql
If ObjectExists("Query", "getDailyCharge") Then
DoCmd.DeleteObject acQuery, "getDailyCharge"
End If
With dbs
.QueryTimeout = 0
Set QueryDef = .CreateQueryDef("getDailyCharge", sql)
End With
strPathToSave = txtOutputPath3.value
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "getDailyCharge", strPathToSave, True
MsgBox "Daily charge report generated."
End If
Else
MsgBox "Input file does not exist. Please enter again."
End If
End If
End Sub
I receive runtime error 3122 :
You tried to execute a query that does not include the specified expression count(*)*t2.Daily_Charge_HKD
as part of the aggregate function
What I want to do in the query:
I want to group all the record in Opt_In_Customer_Record by event_plan_code, and have a total count for each of the code, then I reference the daily_charge from the daily_charge table by t1.event_plan_code = t2.event_plan_code, and multiply the daily_charge with the total count for each of the code
Here is my code:
Private Sub btnGenDaily_Click()
Dim filename As String
Dim prefix As String
Dim qryDef As DAO.QueryDef
Dim dbs As DAO.Database
Set dbs = OpenDatabase(CurrentDb.Name)
If IsNull(txtInputPath.value) Then
MsgBox "Please enter a valid input file location."
Else
If FileExists(txtInputPath.value) Then
If IsNull(txtOutputPath3.value) Then
MsgBox "Please enter a valid output file location."
Else
prefix = GetFileNamePrefix(txtInputPath.value)
sql = "select t1.event_plan_code, count(*), count(*)*t2.Daily_Charge_HKD " & _
"from Opt_In_Customer_Record t1 Inner Join Daily_Charge t2 " & _
"On (t1.event_plan_code=t2.event_plan_code and t2.Home_BMO='" & prefix & "') " & _
"group by t1.event_plan_code " & _
"order by t1.event_plan_code "
MsgBox sql
If ObjectExists("Query", "getDailyCharge") Then
DoCmd.DeleteObject acQuery, "getDailyCharge"
End If
With dbs
.QueryTimeout = 0
Set QueryDef = .CreateQueryDef("getDailyCharge", sql)
End With
strPathToSave = txtOutputPath3.value
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "getDailyCharge", strPathToSave, True
MsgBox "Daily charge report generated."
End If
Else
MsgBox "Input file does not exist. Please enter again."
End If
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道您在评论中提到您不想按“t2.Daily_Charge_HK”进行分组,但要以您访问权限的方式使用它,您将需要对其进行分组,因为您的加入我猜您每个活动计划代码只有一个每日费用值,因此这种情况下的分组不会出现问题。例如,ID 为 1 且 Home BMO 前缀为 x 的所有活动计划代码将具有相同的费率。
更改:
变为:
希望这有帮助
I know you mentioned in a comment that you don't want to group by "t2.Daily_Charge_HK", but to use that in the way you are doing with access you will need to group it, because of your join I'm guessing you only have one Daily Charge value for each event plan code so the grouping in this case won't be a problem. e.g. all event plan codes with id of 1 and Home BMO a prefix of x will have the same charge rate.
Change:
Into:
Hope this helps
最后我想出了这个解决方案并且它有效:
sql = "select t1.event_plan_code, Count, Count*Daily_Charge_HKD As 'Count * Daily_Charge_HKD' " & _
Finally I come up with this solution and it works:
sql = "select t1.event_plan_code, Count, Count*Daily_Charge_HKD As 'Count * Daily_Charge_HKD' " & _