-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.sql
More file actions
176 lines (141 loc) · 4.97 KB
/
Copy pathFunctions.sql
File metadata and controls
176 lines (141 loc) · 4.97 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use IAEA;
set global log_bin_trust_function_creators = 1;
# Функция, возвращающая самую мощную электростанцию в мире, которая функционирует на сегодняшний день
drop function if exists most_powerful_power_plant;
DELIMITER //
create function most_powerful_power_plant()
returns varchar(80)
begin
declare most_powerful varchar(80);
select power_plants_name into most_powerful
from nuclear_power_plants where
(select max(power_plants_capacity)
from nuclear_power_plants) = power_plants_capacity
and power_plants_status_id = 3;
return most_powerful;
end//
DELIMITER ;
select most_powerful_power_plant();
# Функция, возвращающая самое популярное вооружение, используемое странами во всём мире
drop function if exists most_popular_weapon;
DELIMITER //
create function most_popular_weapon()
returns varchar(80)
begin
declare i int;
declare weapon varchar(80);
declare maximum int default 0;
declare current_value int default 0;
select count(*) into i from weapons_types;
while i>0 do
select count(*) into current_value from weaponry_of_countries
where weaponry_of_countries_ref_weapons_types = i;
if maximum < current_value then
set maximum = current_value;
end if;
set i = i - 1;
end while;
select weapons_types_name into weapon from weapons_types
where weapons_types_id = maximum;
return weapon;
end//
DELIMITER ;
select most_popular_weapon();
# Функция, возвращающая крупнейший по производству промышленный центр
drop function if exists largest_industrial_center;
DELIMITER //
create function largest_industrial_center()
returns varchar(80)
begin
declare i int;
declare center varchar(80);
declare center_id int;
declare maximum int default 0;
declare current_value int default 0;
select count(*) into i from largest_production_centers;
while i>0 do
select sum(weapons_warehouse_quantity) into current_value from weapons_warehouse
where weapons_warehouse_ref_production_centers = i;
if maximum < current_value then
set maximum = current_value;
set center_id = i;
end if;
set i = i - 1;
end while;
select production_centers_name into center from largest_production_centers
where production_centers_id = center_id;
return center;
end//
DELIMITER ;
select largest_industrial_center();
# Функция, возвращающая самую конфликтную страну
drop function if exists most_conflicting_country;
DELIMITER //
create function most_conflicting_country()
returns varchar(80)
begin
declare country varchar(80);
declare part_1 varchar(80);
declare part_2 varchar(80);
declare counter_1 int;
declare counter_2 int;
select military_conflicts_conflicting_party_1 into part_1
from military_conflicts
group by military_conflicts_conflicting_party_1
order by count(*) desc
limit 1;
select military_conflicts_conflicting_party_2 into part_2
from military_conflicts
group by military_conflicts_conflicting_party_2
order by count(*) desc
limit 1;
select count(*) into counter_1
from military_conflicts
group by military_conflicts_conflicting_party_1
order by count(*) desc
limit 1;
select count(*) into counter_2
from military_conflicts
group by military_conflicts_conflicting_party_2
order by count(*) desc
limit 1;
if counter_1 >= counter_2 then
select Name into country from countries
where Code = part_1;
else
select Name into country from countries
where Code = part_2;
end if;
return country;
end//
DELIMITER ;
select most_conflicting_country();
# Функция, возвращающая предпреятие-лидер в незаконном обогащении ядерного топлива
drop function if exists enrichment_leader;
DELIMITER //
create function enrichment_leader()
returns varchar(80)
begin
declare leader varchar(80);
select production_centers_name into leader from largest_production_centers
where production_centers_id = (select fuel_enrichment_enterprise from fuel_enrichment
where fuel_enrichment_fuel_amount = (select max(fuel_enrichment_fuel_amount) from fuel_enrichment));
return leader;
end//
DELIMITER ;
select enrichment_leader();
# Функция, возвращающая страну с наибольшим объёмом ядерных отходов
drop function if exists most_waste;
DELIMITER //
create function most_waste()
returns varchar(80)
begin
declare country_waste_leader varchar(80);
select name into country_waste_leader from countries
where code = (select nuclear_waste_country from nuclear_waste
where nuclear_waste_volume = (select max(nuclear_waste_volume) from nuclear_waste)
limit 1);
return country_waste_leader;
end//
DELIMITER ;
select most_waste();