我需要构建一个查询,以使用SQL从以下表中查找以下信息:
查找星期六起飞并于星期日起飞的不同乘客的名字。
以下是数据库架构中的表:
Company table
ID_comp name
------------
1 Don_avia
2 Aeroflot
3 Dale_avia
4 air_France
5 British_AW
Passenger table
ID_psg name
-----------
1 Bruce Willis
2 George Clooney
3 Kevin Costner
4 Donald Sutherland
5 Jennifer Lopez
6 Ray Liotta
7 Samuel L. Jackson
8 Nikole Kidman
9 Alan Rickman
10 Kurt Russell
11 Harrison Ford
12 Russell Crowe
13 Steve Martin
14 Michael Caine
15 Angelina Jolie
16 Mel Gibson
17 Michael Douglas
18 John Travolta
19 Sylvester Stallone
20 Tommy Lee Jones
21 Catherine Zeta-Jones
22 Antonio Banderas
23 Kim Basinger
24 Sam Neill
25 Gary Oldman
26 Clint Eastwood
27 Brad Pitt
28 Johnny Depp
29 Pierce Brosnan
30 Sean Connery
31 Bruce Willis
37 Mullah Omar
Pass_in_trip table
trip_no date ID_psg place
-------------------------
1100 2003-04-29 00:00:00.000 1 1a
1123 2003-04-05 00:00:00.000 3 2a
1123 2003-04-08 00:00:00.000 1 4c
1123 2003-04-08 00:00:00.000 6 4b
1124 2003-04-02 00:00:00.000 2 2d
1145 2003-04-05 00:00:00.000 3 2c
1145 2003-04-25 00:00:00.000 5 1d
1181 2003-04-01 00:00:00.000 1 1a
1181 2003-04-01 00:00:00.000 6 1b
1181 2003-04-01 00:00:00.000 8 3c
1181 2003-04-13 00:00:00.000 5 1b
1182 2003-04-13 00:00:00.000 5 4b
1182 2003-04-13 00:00:00.000 9 6d
1187 2003-04-14 00:00:00.000 8 3a
1187 2003-04-14 00:00:00.000 10 3d
1188 2003-04-01 00:00:00.000 8 3a
7771 2005-11-04 00:00:00.000 11 4a
7771 2005-11-07 00:00:00.000 11 1b
7771 2005-11-07 00:00:00.000 37 1c
7771 2005-11-09 00:00:00.000 11 5a
7771 2005-11-14 00:00:00.000 14 4d
7771 2005-11-16 00:00:00.000 14 5d
7772 2005-11-07 00:00:00.000 12 1d
7772 2005-11-07 00:00:00.000 37 1a
7772 2005-11-29 00:00:00.000 10 3a
7772 2005-11-29 00:00:00.000 13 1b
7772 2005-11-29 00:00:00.000 14 1c
7773 2005-11-07 00:00:00.000 13 2d
7778 2005-11-05 00:00:00.000 10 2a
8881 2005-11-08 00:00:00.000 37 1d
8882 2005-11-06 00:00:00.000 37 1a
8882 2005-11-13 00:00:00.000 14 3d
Trip table
trip_no ID_comp plane town_from town_to time_out time_in
--------------------------------------------------------
1100 4 Boeing Rostov Paris 1900-01-01 14:30:00.000 1900-01-01 17:50:00.000
1101 4 Boeing Paris Rostov 1900-01-01 08:12:00.000 1900-01-01 11:45:00.000
1123 3 TU-154 Rostov Vladivostok 1900-01-01 16:20:00.000 1900-01-01 03:40:00.000
1124 3 TU-154 Vladivostok Rostov 1900-01-01 09:00:00.000 1900-01-01 19:50:00.000
1145 2 IL-86 Moscow Rostov 1900-01-01 09:35:00.000 1900-01-01 11:23:00.000
1146 2 IL-86 Rostov Moscow 1900-01-01 17:55:00.000 1900-01-01 20:01:00.000
1181 1 TU-134 Rostov Moscow 1900-01-01 06:12:00.000 1900-01-01 08:01:00.000
1182 1 TU-134 Moscow Rostov 1900-01-01 12:35:00.000 1900-01-01 14:30:00.000
1187 1 TU-134 Rostov Moscow 1900-01-01 15:42:00.000 1900-01-01 17:39:00.000
1188 1 TU-134 Moscow Rostov 1900-01-01 22:50:00.000 1900-01-01 00:48:00.000
1195 1 TU-154 Rostov Moscow 1900-01-01 23:30:00.000 1900-01-01 01:11:00.000
1196 1 TU-154 Moscow Rostov 1900-01-01 04:00:00.000 1900-01-01 05:45:00.000
7771 5 Boeing London Singapore 1900-01-01 01:00:00.000 1900-01-01 11:00:00.000
7772 5 Boeing Singapore London 1900-01-01 12:00:00.000 1900-01-01 02:00:00.000
7773 5 Boeing London Singapore 1900-01-01 03:00:00.000 1900-01-01 13:00:00.000
7774 5 Boeing Singapore London 1900-01-01 14:00:00.000 1900-01-01 06:00:00.000
7775 5 Boeing London Singapore 1900-01-01 09:00:00.000 1900-01-01 20:00:00.000
7776 5 Boeing Singapore London 1900-01-01 18:00:00.000 1900-01-01 08:00:00.000
7777 5 Boeing London Singapore 1900-01-01 18:00:00.000 1900-01-01 06:00:00.000
7778 5 Boeing Singapore London 1900-01-01 22:00:00.000 1900-01-01 12:00:00.000
8881 5 Boeing London Paris 1900-01-01 03:00:00.000 1900-01-01 04:00:00.000
8882 5 Boeing Paris London 1900-01-01 22:00:00.000 1900-01-01 23:00:00.000
关于如何格式化最后一个表格的任何建议也会受到赞赏,我一直试图在过去一小时内修复它!
答案 0 :(得分:2)
如果我们假设任何航班最多不超过24小时,我们需要做的就是找到星期六开始的航班并在time_in
之前获得time_out
值。使用MSSQL,它看起来像这样:
select p.name
from Pass_in_trip pit
join Trip t on t.trip_no = pit.trip_no
join Passenger p on pit.ID_psg = p.ID_psg
where DATENAME(dw, pit.date) = 'Saturday'
and t.time_in < t.time_out
使用您的样本数据,这将返回:
Kevin Costner
Kurt Russell
当然,如果旅行时间超过24小时,则无法确定航班是在两个日期之间进行的,因为数据模型并没有记录下来。
对于 Oracle ,你可以按照这样的工作日检查to_char(date, 'DAY') = 'Saturday'
我认为 - 我还没有能够测试它。