我有一张桌子,可以跟踪车辆进出的时间表。
Table: Schedule
route location v_type out in
===================================================================
a loc1 10 2/14/2013 08:04:00 2/14/2013 10:03:00
b loc1 11 2/14/2013 08:06:00 2/14/2013 14:20:00
c loc2 11 2/14/2013 06:22:00 2/14/2013 07:50:00
d loc1 10 2/14/2013 11:04:00 2/14/2013 10:03:00
e loc2 10 2/14/2013 08:06:00 2/14/2013 14:20:00
f loc2 11 2/14/2013 06:22:00 2/14/2013 07:50:00
想象一下,每天有数千条路线。我正在尝试找出每个位置,以及 v_type 最多车辆在路上行驶的时间(或时间窗口)。
期望的结果,例如
location v_type time peak
===========================================
loc1 10 2/14/2013 10:40 110
loc1 11 2/14/2013 10:30 80
loc2 10 2/14/2013 08:05 67
loc2 11 2/14/2013 09:45 107
等等
基本思想是,您可以通过查找已离开的车辆总数并减去当天返回的车辆数量来查找任何时间点上道路上的车辆数量。
这是我到目前为止所拥有的,但它不能完全正确地工作,而且速度很慢。
SELECT s.location,
s.v_type,
TO_CHAR(TRUNC(s.out, 'mi') - mod(EXTRACT(minute FROM CAST(s.out AS TIMESTAMP)), 10) / (24 * 60), 'YYYY-MM-DD HH24:MI') AS TIME,
(SELECT
(SELECT COUNT(*)
FROM SCHEDULE s2
WHERE s2.out BETWEEN TRUNC(s.out) AND (TRUNC(s.out, 'mi') - mod(EXTRACT(minute FROM CAST(s.out AS TIMESTAMP)), 10) / (24 * 60))
)
-
(SELECT COUNT(*)
FROM SCHEDULE s2
WHERE s2.out BETWEEN TRUNC(s.in) AND (TRUNC(s.in, 'mi') - mod(EXTRACT(minute FROM CAST(s.in AS TIMESTAMP)), 10) / (24 * 60))
)
FROM dual
)
FROM SCHEDULE s
GROUP BY s.location, s.v_type,
(TRUNC(s.out, 'mi') - mod(EXTRACT(minute FROM CAST(s.out AS TIMESTAMP)), 10) / (24 * 60))