-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunction_web.sql
450 lines (404 loc) · 10.8 KB
/
function_web.sql
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
CREATE OR REPLACE FUNCTION web.lookup_area_bucket_type(i_marine_layer_id int, i_bucket_type_name varchar)
RETURNS int AS
$body$
SELECT area_bucket_type_id FROM web.area_bucket_type WHERE marine_layer_id = i_marine_layer_id AND name = i_bucket_type_name;
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.lookup_area_key(i_marine_layer_id int, i_main_area_id int[])
RETURNS int[] AS
$body$
SELECT array_agg(DISTINCT area_key) FROM web.area WHERE marine_layer_id = i_marine_layer_id AND main_area_id = any(i_main_area_id);
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.lookup_area_key_for_fao(i_fao_area_id int[])
RETURNS int[] AS
$body$
SELECT array_agg(DISTINCT a.area_key)
FROM web.fao_area f
JOIN web.area a ON (a.main_area_id = f.fao_area_id AND a.marine_layer_id = 2)
WHERE f.fao_area_id = ANY(i_fao_area_id);
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.get_area_status
(
i_marine_layer_id int,
i_main_area_id int,
i_sub_area_id int
)
RETURNS boolean AS
$body$
DECLARE
result BOOLEAN := false;
BEGIN
--Exceptions
IF (i_marine_layer_id IN (4, 6)) THEN
result := TRUE;
ELSIF (i_sub_area_id = 0) THEN
IF EXISTS(SELECT 1
FROM web.area a
JOIN web.v_fact_data v ON (a.area_key = v.area_key)
WHERE a.marine_layer_iD = i_marine_layer_id
AND a.main_area_iD = i_main_area_id
LIMIT 1)
THEN
result := TRUE;
END IF;
ELSIF (i_sub_area_id > 0) THEN
IF EXISTS(SELECT area_key
FROM web.area a
WHERE a.marine_layer_id = i_marine_layer_id AND a.main_area_id = i_main_area_id AND a.sub_area_id = i_sub_area_id
AND EXISTS(Select 1 FROM web.v_fact_data v WHERE v.area_key = a.area_key LIMIT 1))
THEN
result := TRUE;
END IF;
END IF;
return result;
END;
$body$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION web.area_get_all_active_combinations()
RETURNS TABLE(marine_layer_id int
,main_area_id int
,sub_area_id int
,area numeric
,ifa numeric
,shelf_area numeric
,coral_reefs numeric
,sea_mounts numeric
,number_of_cells int)
AS
$body$
--for the entire EEZs/LMEs/Global/Mariculture/persian_gulf/Tropics/MEOW
SELECT marine_layer_id
,main_area_id
,0
,SUM(area)
,SUM(ifa)
,SUM(shelf_area)
,SUM(coral_reefs)
,SUM(sea_mounts)
,SUM(number_of_cells)::INT
FROM web.area
WHERE marine_layer_id IN (1,3,6, 8, 9,10,19)
AND web.get_area_status(marine_layer_id, main_area_id, sub_area_id)
GROUP BY main_area_id, marine_layer_id
UNION ALL
--for the rest of everything else
SELECT marine_layer_id
,main_area_id
,sub_area_id
,area
,ifa
,shelf_area
,coral_reefs
,sea_mounts
,number_of_cells
FROM web.area
WHERE web.get_area_status(marine_layer_id, main_area_id, sub_area_id);
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.area_get_all_active_combinations_v1()
RETURNS TABLE(marine_layer_id int
,main_area_id int
,sub_area_id int
,area numeric
,shelf_area numeric
,coral_reefs numeric
,sea_mounts numeric
,number_of_cells int)
AS
$body$
--for the entire EEZs/LMEs/Global/MEOW
(SELECT marine_layer_iD
,main_area_id
,0
,SUM(area)
,SUM(shelf_area)
,SUM(coral_reefs)
,SUM(sea_mounts)
,SUM(number_of_cells)::INT
FROM web.area
WHERE marine_layer_id IN (1,3,6,19)
AND web.get_area_status(marine_layer_id, main_area_id, sub_area_id)
GROUP BY main_area_id, marine_layer_id)
UNION ALL
--for the rest of everything else
(SELECT marine_layer_iD
,main_area_id
,sub_area_id
,Area
,shelf_area
,coral_reefs
,sea_mounts
,number_of_cells
FROM web.area
WHERE web.get_area_status(marine_layer_id, main_area_id, sub_area_id));
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.etl_get_area_catch_allocation_type_key
(
i_legacy_reallocate smallint
)
RETURNS smallint AS
$body$
SELECT CASE i_legacy_reallocate
WHEN 0 THEN 1::SMALLINT
WHEN 1 THEN 2::SMALLINT
END;
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.etl_get_area_key
(
i_marine_layer_id int,
i_main_area_id int,
i_sub_area_id int
)
RETURNS int AS
$body$
SELECT area_key
FROM web.area
WHERE marine_layer_id = i_marine_layer_id
AND main_area_id = i_main_area_id
AND sub_area_id = i_sub_area_id
LIMIT 1;
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.etl_get_fishing_entity_id
(
i_legacy_c_number int
)
RETURNS smallint AS
$body$
SELECT fishing_entity_id
FROM web.fishing_entity
WHERE legacy_c_number = i_legacy_c_number
LIMIT 1;
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.etl_get_time_key
(
i_year int
)
RETURNS int AS
$body$
SELECT time_key
FROM web.time
WHERE time_business_key = i_year
LIMIT 1;
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.etl_validation_get_fao_name
(
i_fao_area_id int
)
RETURNS varchar(50) AS
$body$
SELECT ' [' || coalesce((SELECT Name FROM web.fao_area WHERE fao_area_id = i_fao_area_id LIMIT 1), '') || ']';
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.etl_validation_get_gear_name
(
i_gear_id int
)
RETURNS varchar(50) AS
$body$
SELECT ' [' || coalesce((SELECT name FROM web.gear WHERE gear_id = i_gear_id LIMIT 1), '?') || ']';
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.etl_validation_get_lme_name
(
i_lme_id int
)
RETURNS varchar(50) AS
$body$
SELECT ' [' || coalesce((SELECT name FROM web.lme WHERE lme_id = i_lme_id LIMIT 1), '?') || ']';
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.etl_validation_get_meow_name
(
i_meow_id int
)
RETURNS varchar(50) AS
$body$
SELECT ' [' || coalesce((SELECT name FROM web.meow WHERE meow_id = i_meow_id LIMIT 1), '?') || ']';
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.get_area_primary_production_rate
(
i_marine_layer_id int,
i_main_area_id int,
i_sub_area_id int
)
RETURNS float AS
$body$
DECLARE
result float := NULL;
BEGIN
IF (i_sub_area_id > 0) THEN
SELECT ppr
INTO result
FROM web.area a
WHERE a.marine_layer_id = i_marine_layer_id
AND a.main_area_id = i_main_area_id
AND a.sub_area_id = i_sub_area_id
LIMIT 1;
ELSIF (i_sub_area_id = 0) THEN
SELECT SUM(a.ppr * a.area)/(SUM(a.area))
INTO result
FROM web.area a
WHERE a.marine_layer_id = i_marine_layer_id
AND a.main_area_id = i_main_area_id;
END IF;
RETURN coalesce(result, 0);
END
$body$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION web.get_area_primary_production
(
i_marine_layer_id int,
i_main_area_id int,
i_sub_area_id int,
i_area float
)
RETURNS float
AS
$body$
SELECT web.get_area_primary_production_rate(i_marine_layer_id, i_main_area_id, i_sub_area_id) * i_area * 365 * 1e6 *9 / (1e9);
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.get_area_url_token
(
i_marine_layer_id int,
i_main_area_id int,
i_sub_area_id int
)
RETURNS varchar(50) AS
$body$
DECLARE
result varchar(50);
BEGIN
SELECT (name || '/' || i_main_area_id) INTO result FROM web.marine_layer WHERE marine_layer_id = i_marine_layer_id LIMIT 1;
IF i_sub_area_id <> 0 THEN
result := result || '_' || i_sub_area_id;
END IF;
RETURN result;
END;
$body$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION web.get_marine_entities
(
i_marine_layer_id int
)
RETURNS TABLE(area_key int,
main_area_id int,
sub_area_id int)
AS
$body$
--for the entire EEZs/LMEs/MEOWs
SELECT area_key, main_area_id, sub_area_id
FROM web.area
WHERE (marine_layer_id = i_marine_layer_id) AND (web.get_area_status(marine_layer_id, main_area_id, sub_area_id));
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.get_eez()
RETURNS TABLE(area_key int,
main_area_id int,
sub_area_id int)
AS
$body$
--for the entire EEZs/LMEs/MEOWs
SELECT * FROM web.get_marine_entities(1);
$body$
LANGUAGE sql;
/* Why 3 input parameters for this function. Only i_main_area_id is used ever */
CREATE OR REPLACE FUNCTION web.get_geo_entity_id
(
i_marine_layer_id int,
i_main_area_id int,
i_sub_area_id int
)
RETURNS int AS
$body$
SELECT CASE
WHEN i_marine_layer_id = 1 THEN (SELECT geo_entity_id FROM web.eez e WHERE e.eez_id = i_main_area_id LIMIT 1)
ELSE NULL
END;
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.get_high_sea()
RETURNS TABLE(area_key int,
main_area_id int,
sub_area_id int)
AS
$body$
--for the entire EEZs/LMEs/MEOWs
SELECT * FROM web.get_marine_entities(2);
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.get_lme()
RETURNS TABLE(area_key int,
main_area_id int,
sub_area_id int)
AS
$body$
--for the entire EEZs/LMEs/MEOWs
SELECT * FROM web.get_marine_entities(3);
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.get_meow()
RETURNS TABLE(area_key int,
main_area_id int,
sub_area_id int)
AS
$body$
--for the entire EEZs/LMEs/MEOWs
SELECT * FROM web.get_marine_entities(19);
$body$
LANGUAGE sql;
CREATE OR REPLACE FUNCTION web.ppr
(
i_total_catch float,
i_tl float
)
RETURNS float AS
$body$
DECLARE
result float;
BEGIN
--SET i_total_catch = i_total_catch * 1e6 -- covert ton to grams
result := (i_total_catch / 9) * power(10, i_tl-1);
--SET result = result / 1e12 -- PPR is expressed in billion G x C
result := result * 9; -- to comply with previous data model
RETURN result;
END
$body$
LANGUAGE plpgsql;
create or replace function web.update_subsidy_landed_value(i_for_year int) returns void as
$body$
WITH lv AS (
SELECT w.geo_entity_id,
SUM(e.total_catch * d.unit_price) AS landed_value
FROM allocation.allocation_result_eez e
JOIN allocation.allocation_data d ON (d.universal_data_id = e.universal_data_id AND d.year = i_for_year)
JOIN web.eez w ON (w.eez_id = e.eez_id)
WHERE e.eez_id > 0
GROUP BY w.geo_entity_id
)
UPDATE web.subsidy s
SET landed_value = lv.landed_value/1000.00
FROM lv
WHERE s.geo_entity_id = lv.geo_entity_id
AND s.year = i_for_year;
$body$
language sql;
--M.Nevado
--8.7.2020
create or replace function web.etl_validation_get_taxon_name
(
i_taxon_key int
)
returns varchar(50) as
$body$
select ' [' || coalesce((select common_name from web.v_dim_taxon where taxon_key = i_taxon_key limit 1), '') || ']';
$body$
language sql;