5

我使用 Ms 访问作为我的数据库,并且我正在使用以下查询来获得工作时间:

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.

任何我犯错误的建议。

4

2 回答 2

5

你有几个错误,以防你试图做一个GROUP BY. 首先查看MSDN 中的 GROUP BY 语法、建议和示例

基本上,无需深入探讨,如果您使用,则子句上不受聚合函数影响的GROUP BY任何列,如、等,都应该出现在子句中。因此,在您的情况下,您应该添加:SELECTSUMAVGGROUP 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. 然后应该像替换一样简单GROUPORDER它应该可以工作。只要确定这是你想要的。

于 2012-09-14T08:15:03.397 回答
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
于 2012-09-14T09:42:00.050 回答