I need to construct a query to find the following information from the below tables using SQL:
Find the names of the different passengers who have ever flown a flight which took off on Saturday and landed on Sunday.
Here are the tables in the database schema:
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
Any suggestions on how to format the last table would be appreciated as well, I have been trying to fix it for the past hour!
If we assume that no flight takes more than 24 hours at the most all we need to do is to find the flights that started of Saturdays and have a time_in
value before the time_out
. With MSSQL it could look like this:
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
With your sample data this would return:
Kevin Costner
Kurt Russell
Of course, if a trip could take more than 24 hours it would be impossible to determine with certainty that the flight took place between two dates as the data model doesn't record this.
For Oracle you can do the weekday check like this to_char(date, 'DAY') = 'Saturday'
I think - I haven't been able to test it though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.