我收到运行时错误3122:
您尝试执行不包含指定表达式count(*)*t2.Daily_Charge_HKD
的查询作为聚合函数的一部分
我想在查询中做些什么:
我想通过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
答案 0 :(得分:0)
我知道你在评论中提到你不想按“t2.Daily_Charge_HK”进行分组,但是要按照你对访问的方式使用它,你需要对它进行分组,因为你的加入我'猜测每个事件计划代码只有一个每日费用值,因此在这种情况下分组不会有问题。例如所有事件计划代码的id为1,而Home BMO的前缀为x将具有相同的费率。
变化:
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 "
分为:
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, t2.Daily_Charge_HKD " & _
"order by t1.event_plan_code, t2.Daily_Charge_HKD "
希望这有帮助
答案 1 :(得分:0)
最后,我提出了这个解决方案并且有效:
sql =“select t1.event_plan_code,Count,Count * Daily_Charge_HKD As'Count * Daily_Charge_HKD'”& _
"from (select event_plan_code, count(*) As Count " & _
"from Opt_In_Customer_Record " & _
"group by event_plan_code " & _
"order by event_plan_code) t1, Daily_Charge t2 " & _
"where t1.event_plan_code=t2.event_plan_code and t2.Home_BMO='" & prefix & "' " & _
"order by t1.event_plan_code"