-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path301.do.update--record-list-with-calculated-values.fn.sql
More file actions
66 lines (65 loc) · 2.21 KB
/
301.do.update--record-list-with-calculated-values.fn.sql
File metadata and controls
66 lines (65 loc) · 2.21 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
create or replace function calc_after_add() returns numeric as $$
declare
pe record;
first_tier3_time int :=0;
first_pair_tier3_time int :=0;
eatst_value int :=0;
begin
--
-- retrieve all records
--
-- 1) in a system with active data addition this would check for nulls so it would only
-- run on new records, and probably be trigger controlled not to mention, there'd be a
-- case for "calculate on add"
-- 2) in my case, the SQL is just re-run and the data-set is small, so I omitted that and,
-- I could have kept "calculate on add" but the file was becoming unwieldy with a lot of
-- cut and paste duplication
--
for pe in
select
thlogepen_id,
tier5_ymto_two,
tier5_ymto_one,
lv2a_ymto_1,
lv2a_ymto_1_1_two,
laf_ymto_1,
laf_ymto_2,
lv2a_ymto_1_2,
lv2a_ymto_2,
otc,
gnsis_tlen,
tier3_ymto_1,
tier3_ymto_2,
tier3_ymto_3,
cover_tlen,
sec_of_lv1_ex
from
thlogepen pe
loop
first_tier3_time = calc_lv3_tier3_ymto(pe);
first_pair_tier3_time = calc_lv3_tier3_ymto_pr(pe);
eatst_value = calc_eatst(pe);
update
thlogepen
set
tier5_ymto_tlen = calc_tlen(pe.tier5_ymto_one, pe.tier5_ymto_two),
lv2a_ymto_1_tlen = calc_lv2a_ymto_1_tlen(pe),
lv2a_ymto_2_tlen = calc_tlen(pe.lv2a_ymto_2, eatst_value),
tier3_ymto_1_tlen = calc_tlen(pe.tier3_ymto_1, eatst_value),
tier3_ymto_2_tlen = calc_tlen(pe.tier3_ymto_2, eatst_value),
tier3_ymto_3_tlen = calc_tlen(pe.tier3_ymto_3, eatst_value),
first_tier3_ymto = first_tier3_time,
first_tier3_ymto_type = calc_lv3_tier3_ymto_type(pe, first_tier3_time),
tier3_ymto_any = tier3_ymto_sec(pe),
full_tier3_ymto_tlen = calc_tlen(first_tier3_time, eatst_value),
tier3_ymto_tlen = calc_tlen(first_pair_tier3_time, eatst_value),
tier3_ymto_proportion = first_tier3_time::float / eatst_value::float,
cover_proportion = pe.cover_tlen::float / calc_survived_vew_tlen(pe)::float,
thlogepen_tlen = calc_tlen(0, eatst_value),
vew_record_is_complete = calc_vew_record_is_complete(pe)
where
thlogepen_id = pe.thlogepen_id;
end loop;
return null;
end;
$$ language plpgsql;