Я бы создал представление.
create view suspicious_person_movements as
select IO.holder_name,
cast(IO.io_date || ' ' || IO.io_time as timestamp) as io_timestamp,
-- Concatenate most recent earlier date and time, and cast to timestamp
cast((select max(io_date)
from iodata
where holder_name = IO.holder_name
and io_date <= IO.io_date)
|| ' ' ||
(select max(io_time)
from iodata
where holder_name = IO.holder_name
and io_date <= IO.io_date
and io_time < IO.io_time) as timestamp) as previous_timestamp,
-- Subtract the timestamps to get elapsed_time
cast(IO.io_date || ' ' || IO.io_time as timestamp) -
cast((select max(io_date)
from iodata
where holder_name = IO.holder_name
and io_date <= IO.io_date) || ' ' ||
(select max(io_time)
from iodata
where holder_name = IO.holder_name
and io_date <= IO.io_date
and io_time < IO.io_time) as timestamp) as elapsed_time,
IO.io_gate_name,
IO.io_status
from iodata IO
order by holder_name, io_date, io_time -- Better to sort in the client?
Тогда я могу просто выбрать все строки из suspicious_person_movements. (Слегка отредактировано для уменьшения горизонтальной прокрутки.)
Dinesh 2010-07-09 00:50:05 Basement(I/O) Entry
Dinesh 2010-07-09 00:52:55 2010-07-09 00:50:05 00:02:50 Basement(I/O) Exit
Dinesh 2010-07-09 01:00:07 2010-07-09 00:52:55 00:07:12 Basement(I/O) Entry
Dinesh 2010-07-09 01:35:42 2010-07-09 01:00:07 00:35:35 Basement(I/O) Exit
Dinesh 2010-07-09 01:36:37 2010-07-09 01:35:42 00:00:55 Ground Fl(I/O) Entry
Dinesh 2010-07-09 01:37:02 2010-07-09 01:36:37 00:00:25 Ground Fl(I/O) Exit
Dinesh 2010-07-09 01:46:04 2010-07-09 01:37:02 00:09:02 Ground Fl(I/O) Entry
Dinesh 2010-07-09 01:46:29 2010-07-09 01:46:04 00:00:25 Ground Fl(I/O) Exit
Dinesh 2010-07-09 01:47:02 2010-07-09 01:46:29 00:00:33 Basement(I/O) Entry
Dinesh 2010-07-09 04:09:11 2010-07-09 01:47:02 02:22:09 Basement(I/O) Exit
Dinesh 2010-07-09 04:09:35 2010-07-09 04:09:11 00:00:24 Ground Fl(I/O) Entry
Dinesh 2010-07-09 04:11:27 2010-07-09 04:09:35 00:01:52 Ground Fl(I/O) Exit
Dinesh 2010-07-09 04:11:54 2010-07-09 04:11:27 00:00:27 Basement(I/O) Entry
Dinesh 2010-07-09 05:10:28 2010-07-09 04:11:54 00:58:34 Ground Fl(I/O) Entry
Dinesh 2010-07-09 05:18:12 2010-07-09 05:10:28 00:07:44 Main Door(I/O) Exit
Dinesh 2010-07-09 17:55:16 2010-07-09 05:18:12 12:37:04 Main Door(I/O) Entry
Dinesh 2010-07-09 17:56:10 2010-07-09 17:55:16 00:00:54 Ground Fl(I/O) Entry
Я реализовал вид самым очевидным способом. Вам понадобятся указатели на имя владельца, дату и время. Вы можете повысить производительность, присоединив таблицу к себе, а не используя эти скалярные подзапросы. В любом случае, производительность большого набора данных может быть невелика. (Но обычно вы можете ограничить имя владельца и диапазон дат, что поможет.)
Для Access вы бы использовали запрос, похожий на этот, который я скопировал из представления SQL.
SELECT IO.holder_name
, [io_date] & " " & [io_time] AS io_timestamp
, (select max(io_date)
from iodata
where holder_name = IO.holder_name
and io_date <= IO.io_date)
& " " &
(select max(io_time)
from iodata
where holder_name = IO.holder_name
and io_date <= IO.io_date
and io_time < IO.io_time) AS previous_timestamp
, DateDiff("n",[previous_timestamp],[io_timestamp]) AS elapsed_minutes
, IO.io_gate_name
, IO.io_status
FROM iodata AS IO
ORDER BY IO.holder_name, IO.io_date, IO.io_time;
Я взял несколько ярлыков, потому что я на работе. Я отображаю прошедшие минуты вместо истекшего времени в формате «00:00:00». Я игнорирую нулевую метку времени для первой строки.