-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.sql
More file actions
66 lines (61 loc) · 1.5 KB
/
Functions.sql
File metadata and controls
66 lines (61 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- passengers should ideally arrive three hours before flight
delimiter $$
create function passenger_arrival(board time)
returns time deterministic
begin
declare interval_time time;
declare arrival_time time;
select '03:00:00' into interval_time;
select subtime(board, interval_time) into arrival_time;
if (board < '03:00:00') then
set arrival_time = addtime(arrival_time, '24:00:00');
end if;
return arrival_time;
end$$
delimiter ;
-- find time in flight
delimiter $$
create function in_flight_time(dept time, arr time)
returns time deterministic
begin
declare result time;
select timediff(arr, dept) into result;
if (result < '00:00:00') then
set result = addtime(result, '24:00:00');
end if;
return result;
end $$
delimiter ;
-- find age of passengers
delimiter $$
create function age(dob date)
returns int deterministic
begin
declare today date;
select current_Date() into today;
return year(today) - year(dob);
end $$
delimiter ;
-- find number of flights taking off a specific terminal
delimiter $$
create function flight_terminal(terminal varchar(2))
returns int deterministic
begin
declare num_flights int;
select count(Ticket_id) from Terminal where Terminal_no = terminal
into num_flights;
return num_flights;
end $$
delimiter ;
-- find if pilot is fit for flying
delimiter $$
create function can_fly(age int)
returns varchar(3) deterministic
begin
if (age >= 25 AND age<=60) then
return "YES";
else
return "NO";
end if;
end $$
delimiter ;