나는보기를 만들 것입니다.
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"형식으로 표시합니다. 나는 첫 번째 행에 대한 null 타임 스탬프를 무시합니다.
우선 IODate와 IOTime을 단일 IODateTime 필드에 결합해야합니다. 날짜와 시간을 두 개의 열로 나누어 불필요하게 더 어렵게 만들었습니다. 제트 엔진은 시간을 나타내는 십진수로 float 형으로 datetimes를 저장합니다. 하루 이상의 시간 차이를 계산하는 것은 현재 설정에서 악몽이 될 것입니다. – mwolfe02