访问sql查询聚合函数

时间:2012-10-15 15:27:33

标签: sql ms-access-2007

我试图在Access 2007中运行一个sql,它给出了错误

“您尝试执行不包含指定表达式'round(volumePass / Volume * 100,2)'的查询作为聚合函数的一部分”。

我在sql语句中提到过它

分组(volumePass / Volume * 100,2)

SELECT s.[sub process name], Round(Avg(s.[TollGate FTEs]),2) AS TollGateFTEs, 
Sum(w.volume) AS Volume, 
Sum(Switch(w.TAT='Pass',w.Volume,w.TAT='Fail',0)) AS VolumePass, 
Sum(IIf(w.[combined accuracy]='PASS',1,0)) AS AccuracyPass, 
Sum(IIf(w.[combined accuracy]='',0,1)) AS TotalAccuracy, 
Round((VolumePass/Volume)*100,2) AS TATPercentage, 
Round((AccuracyPass/TotalAccuracy)*100,2) AS AccuracyPercentage, 
Format(w.[reporting month],'mmm-yy') AS [Rep Month] FROM Work_Log1 AS w, 
[sub process] AS s WHERE w.[sub process] In (SELECT s.[sub_process_id] FROM 
[Sub Process] s, 
[process mapping] m where m.[process name] like 'Accounting Control%'  
and s.[mapped to process id] = m.[mapping id]) And w.[sub process]=[s].[sub_process_id] 
AND (w.[Activity_start_date] Between #06/01/2012# And #06/15/2012#)  
AND ([w].[sla applicable]=1 Or 0 Or '') and 
(w.[status] Like 'Complete%')  group BY Format(w.[reporting month],'mmm-yy'), 
s.[sub process name], (Round((VolumePass/Volume)*100,2));

它无法正确拾取。

给别名“ TATPercentage ”仍然不起作用并给出相同的错误。

此查询在访问查询设计器中运行良好,但无法在vba中使用sql运行。

更新

让它在vba中使用dao引擎运行..没有运气adodb引擎

1 个答案:

答案 0 :(得分:1)

Round((VolumePass/Volume)*100,2) AS TATPercentage
       ^^^^^^^^^^ ^^^^^^

round()不是聚合函数。您还必须对这些字段进行分组(音量通过和音量),并且您将在接下来的两行中遇到相同的错误,包括accuracypass,acuracytotal,report month等...