Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 如何解决此查询?_Ms Access_Vba - Fatal编程技术网

Ms access 如何解决此查询?

Ms access 如何解决此查询?,ms-access,vba,Ms Access,Vba,我收到运行时错误3122: 您试图执行的查询未将指定的表达式count(*)*t2.Daily\u Charge\u HKD作为聚合函数的一部分 我想在查询中执行的操作: 我想按事件计划代码对Opt_in_Customer_record中的所有记录进行分组,并对每个代码进行总计数,然后通过t1.event_plan_code=t2.event_plan_code引用daily_charge表中的daily_charge,并将daily_charge乘以每个代码的总计数 这是我的密码: Priva

我收到运行时错误3122:

您试图执行的查询未将指定的表达式
count(*)*t2.Daily\u Charge\u HKD
作为聚合函数的一部分

我想在查询中执行的操作:

我想按事件计划代码对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

我知道你在评论中提到,你不想按“t2.Daily_Charge_HK”分组,但要按访问方式使用,你需要对其进行分组,因为你的加入,我猜你对每个活动计划代码只有一个每日收费值,所以在这种情况下分组不会有问题。e、 g.id为1且家庭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=“选择t1.event\u plan\u code、Count、Count*Daily\u Charge\u HKD作为'Count*Daily\u Charge\u HKD'”_


可能是因为您没有按t2分组。每日费用HKD?是的,但我不想按此分组。这将创建一个不同的组。如果它不是分组字段,则不能在选择中直接使用该字段;该字段只能用于汇总功能,如sum、avg、min、max等。。
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 "
  "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"