Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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 - Fatal编程技术网

Ms access 如何计算时间?

Ms access 如何计算时间?,ms-access,Ms Access,使用MS Access 2007 表1 ID日期时间 001, 20091111, 141625 001, 20091112, 122345 001, 20091112, 180000 001, 20091113, 100000 001, 20091113, 120000 001, 20091113, 180000 001, 20091113, 160000 ..., 从上表中,我想再添加一列作为状态。状态应为In或Out。 也就是说 对于20091111-只有一次,因此应为141625-英寸

使用MS Access 2007

表1

ID日期时间

001, 20091111, 141625
001, 20091112, 122345
001, 20091112, 180000
001, 20091113, 100000
001, 20091113, 120000
001, 20091113, 180000
001, 20091113, 160000
...,
从上表中,我想再添加一列作为状态。状态应为In或Out。 也就是说 对于20091111-只有一次,因此应为141625-英寸 对于20091112-2次,它应该是122345-In,180000-Out 对于20091113-4次,因此10000英寸,120000英寸,180000英寸,160000英寸

也就是说

Date with one time means, the time should be In, 
Date with two times means, the First time should be In, Second time should be out
Date with three times means, the first time should be In, Second time should be out,
third time should be In.
Date with four times means, the first time should be In, Second time should be out,
third time should be In, Fourth time should be out.
比如说

Date Time 

20091010 100000
20091010 180000
20091010 120000
它应该按升序排列日期,然后显示第一个输入,第二个输出,第三个输入

输出

Date Time Status

20091010 100000 In
20091010 180000 In
20091010 120000 Out
预期产量

ID日期时间状态

001, 20091111, 141625 In
001, 20091112, 122345 In
001, 20091112, 180000 Out
001, 20091113, 100000 In
001, 20091113, 120000 Out
001, 20091113, 180000 Out
001, 20091113, 160000 In
...,
如何对此条件进行访问查询

需要查询帮助

0)将所有记录的状态设置为空

update table1 set status=Null;
1) 为每个日期的第一条记录分配“In”:

update Table1 as tab_outer set status='In' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date);
2) 为每个无状态的日期的第一条记录分配“Out”:

update Table1 as tab_outer set status='Out' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);
update Table1 as tab_outer set status='In' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);
update Table1 as tab_outer set status='Out' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);
3) 为每个无状态的日期的第一条记录分配“In”:

update Table1 as tab_outer set status='Out' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);
update Table1 as tab_outer set status='In' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);
update Table1 as tab_outer set status='Out' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);
4) 为每个无状态的日期的第一条记录分配“Out”:

update Table1 as tab_outer set status='Out' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);
update Table1 as tab_outer set status='In' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);
update Table1 as tab_outer set status='Out' where Time=
 (select min(Time) from Table1 as tab_inner 
 where tab_inner.date=tab_outer.date and Time is null);

有时,无法针对模式编写简单查询(错误代码…)是一种代码“味道”。在这种情况下,您的设计是有缺陷的(…由错误代码引起)

您的设计存在更新异常。考虑删除“OUT”行将导致实体的后续行隐式地(错误地)反转。
您应该在同一行中同时有
输入日期
输出日期
列。然后,您需要与设计相关的所有约束,即,
out\u date
不能早于
in\u date
,同一实体没有重叠的时段,等等。

您需要子选择来实现这一点。我不熟悉MS Access 2007 SQL,但您可以将其转换为正确的语法:

SELECT ID, Date, Time, 
CASE ((SELECT count(*) FROM table1 as subQuery WHERE subQuery.Date = outerQuery.Date AND subQuery.Time > outerQuery.Time order by Time) % 2)
  WHEN 0 THEN 'In'  
  ELSE 'Out'
END as Status
FROM table1 AS outerQuery
subselect将查询外部选择时间和日期的前置行数。然后,如果结果为0(前导的偶数)或1(前导的奇数),则CASE语句将使用模运算符对该排名进行评估,以打印In,或打印Out


希望这能有所帮助……

也许我只是瞎了眼,但我看不出你的解释背后有什么道理。决定是使用In还是Out的算法是什么?这些是日期字段还是文本字段还是数字?但我想作为单个查询进行查询,是否可以作为单个查询进行查询