【问题标题】:Need help finding the next record in access需要帮助查找访问中的下一条记录
【发布时间】:2013-04-16 18:33:48
【问题描述】:

这是我的桌子

VehicleName EventDate       Message  Landmark
071-314     28-Sep-12 5pm   On Duty 
071-314     28-Sep-12 6pm   Driving 
071-314     28-Sep-12 7pm   On Duty 
071-314     28-Sep-12 8pm   Driving 
071-314     28-Sep-12 3am   Off Duty    
071-315     28-Sep-12 4am   Driving 
071-315     28-Sep-12 5am   On Duty 
071-315     28-Sep-12 6am   Driving 
071-315     28-Sep-12 7am   On Duty 
071-315     28-Sep-12       Off Duty    

我需要做的是找出从“上班”到下一个“驾驶”或“下班”状态的区别。它也需要用车辆名称分隔。

这可以通过访问来实现吗?

【问题讨论】:

  • 有什么区别?时差?
  • 请告诉我你的EventDate 不像28-Sep-12 7am
  • 是的,我需要时差。
  • 不,这是全职,我只是将其截断以适应

标签: sql ms-access


【解决方案1】:

我将您的示例数据放入名为 [DutyEvents] 的表中,并对其进行了调整以使其保持一致(例如,第一个 [VehicleName] 的最终“下班时间”是在第二天凌晨 3 点) ...

VehicleName EventDate            Message
----------- -------------------  -------
071-314     2012-09-28 17:00:00  On Duty
071-314     2012-09-28 18:00:00  Driving
071-314     2012-09-28 19:00:00  On Duty
071-314     2012-09-28 20:00:00  Driving
071-314     2012-09-29 03:00:00  Off Duty
071-315     2012-09-28 04:00:00  Driving
071-315     2012-09-28 05:00:00  On Duty
071-315     2012-09-28 06:00:00  Driving
071-315     2012-09-28 07:00:00  On Duty
071-315     2012-09-28 09:00:00  Off Duty

...然后我创建了以下 Access 查询...

SELECT VehicleName, Message, EventDate AS Start, NextEventDate as End, 
    Round((NextEventDate - EventDate) * 24, 0) AS Hours
FROM
(
    SELECT de.VehicleName, de.EventDate, de.Message,
        (SELECT TOP 1 EventDate FROM DutyEvents 
         WHERE VehicleName = de.VehicleName
            AND EventDate > de.EventDate ORDER BY EventDate
        ) AS NextEventDate
    FROM DutyEvents de
    WHERE de.Message <> "Off Duty"
) 

...产生以下结果...

VehicleName Message  Start                End                   Hours
----------- -------  -------------------  -------------------   -----
071-314     On Duty  2012-09-28 17:00:00  2012-09-28 18:00:00   1
071-314     Driving  2012-09-28 18:00:00  2012-09-28 19:00:00   1
071-314     On Duty  2012-09-28 19:00:00  2012-09-28 20:00:00   1
071-314     Driving  2012-09-28 20:00:00  2012-09-29 03:00:00   7
071-315     Driving  2012-09-28 04:00:00  2012-09-28 05:00:00   1
071-315     On Duty  2012-09-28 05:00:00  2012-09-28 06:00:00   1
071-315     Driving  2012-09-28 06:00:00  2012-09-28 07:00:00   1
071-315     On Duty  2012-09-28 07:00:00  2012-09-28 09:00:00   2

这就是你要找的吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2014-08-15
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多