Oracle查询根据条件

时间:2015-08-06 08:22:26

标签: sql oracle case

我有如下表格。

enter image description here如下所示

我需要的结果是不同列中的IN和OUT,如RESULT表中所示。如果特定车辆的第一个条目是 IN ,那么我需要立即 OUT ,否则将其保持空白。 但是,如果特定车辆的第一个记录是 OUT ,那么我需要获得上一个日期的该车辆的最后 IN 记录,如果存在的话(例如图中的示例) veh2 )。

有没有办法使用oracle查询?无论如何,任何人都可以帮助我吗?提前谢谢。

数据为文字: 08/04/15 veh1在08:00:00 AM

08/04/15 veh1在08:15:00 AM

08/04/15 veh1 out 08:17:00 AM

08/04/15 veh1在08:20:00 AM

08/04/15 veh1 out 08:50:00 AM

08/04/15 veh1于09:15:00 AM

08/04/15 veh1 out 09:30:00 AM

08/04/15 veh1在10:15:00 AM

08/04/15 veh2 out 08:00:00 AM

08/04/15 veh2在08:15:00 AM

08/04/15 veh2 out 09:50:00 AM

08/04/15 veh2在10:10:00 AM

我在数据类型为Date的同一列中有时间和日期。

1 个答案:

答案 0 :(得分:1)

SQL Fiddle

架构详细信息

create table vehicle_entry
(dt date,
 vehicle_no varchar2(20),
 entry_mode varchar2(10),
 entry_date varchar2(20));

 insert into vehicle_entry values('05-Aug-2015','v1','IN','08:00:00 AM');
 insert into vehicle_entry values('05-Aug-2015','v1','IN','08:15:00 AM');
 insert into vehicle_entry values('05-Aug-2015','v1','OUT','08:17:00 AM');
 insert into vehicle_entry values('05-Aug-2015','v1','IN','08:20:00 AM');
 insert into vehicle_entry values('05-Aug-2015','v1','OUT','08:50:00 AM');
 insert into vehicle_entry values('05-Aug-2015','v1','IN','10:15:00 AM');
 insert into vehicle_entry values('04-Aug-2015','v2','IN','10:12:00 PM');
 insert into vehicle_entry values('05-Aug-2015','v2','OUT','08:20:00 AM');

选择查询

select to_char(complete_date, 'mm/dd/rrrr') as dt,vehicle_no, in_date,(select out_date from
(select rownum as rn,vehicle_no, entry_mode, to_date(to_char(dt,'dd-Mon-rrrr') || ' ' || entry_date, 'dd-Mon-rrrr hh:mi:ss AM') complete_date,
case when entry_mode='IN' then to_date(to_char(dt,'dd-Mon-rrrr') || ' ' || entry_date, 'dd-Mon-rrrr hh:mi:ss AM') else null end as in_date,
case when entry_mode='OUT' then to_date(to_char(dt,'dd-Mon-rrrr') || ' ' || entry_date, 'dd-Mon-rrrr hh:mi:ss AM') else null end as out_date
from vehicle_entry
order by vehicle_no, complete_date) b where b.rn=a.rn+1 and b.vehicle_no=a.vehicle_no
) as out_date
from
(
select rownum as rn,vehicle_no, entry_mode, to_date(to_char(dt,'dd-Mon-rrrr') || ' ' || entry_date, 'dd-Mon-rrrr hh:mi:ss AM') complete_date,
case when entry_mode='IN' then to_date(to_char(dt,'dd-Mon-rrrr') || ' ' || entry_date, 'dd-Mon-rrrr hh:mi:ss AM') else null end as in_date,
case when entry_mode='OUT' then to_date(to_char(dt,'dd-Mon-rrrr') || ' ' || entry_date, 'dd-Mon-rrrr hh:mi:ss AM') else null end as out_date
from vehicle_entry
order by vehicle_no, complete_date
) a
where in_date is not null
order by vehicle_no,1

<强>输出

|         DT | VEHICLE_NO |                  IN_DATE |                 OUT_DATE |
|------------|------------|--------------------------|--------------------------|
| 08/05/2015 |         v1 | August, 05 2015 08:00:00 |                   (null) |
| 08/05/2015 |         v1 | August, 05 2015 08:15:00 | August, 05 2015 08:17:00 |
| 08/05/2015 |         v1 | August, 05 2015 08:20:00 | August, 05 2015 08:50:00 |
| 08/05/2015 |         v1 | August, 05 2015 10:15:00 |                   (null) |
| 08/04/2015 |         v2 | August, 04 2015 22:12:00 | August, 05 2015 08:20:00 |