您尝试执行不包含指定表达式'out_time'的查询作为ms访问中的聚合函数

时间:2012-09-14 07:25:30

标签: sql ms-access

我使用Ms access作为我的数据库,我正在使用以下查询来获取时间:

select 
        in_time,
        out_time,
        datediff("n",b.in_time,c.out_time) as work_time,
        log_date,
        emp_id 
from 
    (select 
        LogTime as in_time,
        SrNo,
        LogID as emp_id,
        LogDate as log_date 
    from LogTemp 
    where Type='IN' ) as b
left join
    (select 
        SrNo as out_id, 
        LogTime as out_time,
        LogID as out_emp_id,
        LogDate as out_log_date 
      from LogTemp 
     where Type = 'OUT'
     group by SrNo) as c
on (b.SrNo <> c.out_id
    and b.emp_id = c.out_emp_id
    and b.log_date = out_log_date ) 
where  
    c.out_id > b.SrNo and 
    [log_date] >= #8/20/2012# and 
    [log_date] <= #8/20/2012# and 
    emp_id = "8" 
group by b.SrNo; 

但是当我执行查询时,我得到以下错误:

"you tried to execute a query that does not include the specified expression 'out_time'
 as an aggregate function in ms access" error.

我犯错误的任何建议。

2 个答案:

答案 0 :(得分:5)

如果您尝试GROUP BY,则会遇到一些错误。首先检查MSDN for GROUP BY syntax, recommendations and samples

基本上,如果使用GROUP BY,如果不加深,SELECT子句中的任何列都不会受到SUMAVG等聚合函数的影响出现在GROUP BY子句中。所以在你的情况下你应该添加:

LogTime as out_time,
LogID as out_emp_id,
LogDate as out_log_date

进入第二个子查询的GROUP BY。并添加

 in_time,
 out_time,
 datediff("n",b.in_time,c.out_time) as work_time,
 log_date,
 emp_id 

在最后的主GROUP BY上。

但是,正如已经在一条评论中指出的那样,也许您想要做的是ORDER BY。然后应该像GROUP替换ORDER一样简单,它应该有效。只要确定你想要的是什么。

答案 1 :(得分:1)

LEFT JOIN中的派生表C不需要任何排序或分组。我无法理解为什么它与FROM的派生表B不匹配。

left join
    (select 
        SrNo as out_id, 
        LogTime as out_time,
        LogID as out_emp_id,
        LogDate as out_log_date 
      from LogTemp 
     where Type = 'OUT') as c

外部查询的最终语句应该是ORDER BY(如上所述),因为外部查询没有任何聚合函数。

我怀疑在与MS Access不匹配时显式连接会有问题,因此您可能要考虑将其移至WHERE语句。

 on (b.SrNo <> c.out_id