2011-05-27 67 views
0

我收到運行時錯誤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

難道是因爲你不是由't2.Daily_Charge_HKD'分組嗎? – 2011-05-27 03:10:42

+0

是的,但在這裏我不想由此分組。這使得一個不同的組。 – lamwaiman1988 2011-05-27 03:14:48

+4

如果它不是分組字段,則不能直接在select中使用該字段;該字段只能用於sum,avg,min,max等彙總函數。 – 2011-05-27 03:17:47

回答

0

我知道你,你不要被「t2.Daily_Charge_HK」希望集團的評論中提到,但使用的是在你的方式與訪問,您將做需要對它進行分組,因爲你的加入我猜你每個活動計劃代碼只有一個Daily Charge值,所以在這種情況下分組不會成爲問題。例如所有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 " 

希望這有助於

0

最後我想出了這個解決方案,它的工作原理:

SQL =「選擇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"