-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path302.do.update--sv.fn.sql
More file actions
411 lines (371 loc) · 8.36 KB
/
302.do.update--sv.fn.sql
File metadata and controls
411 lines (371 loc) · 8.36 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
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
create or replace function update_sv() returns numeric as $$
declare
e_c_1_sv int := 0;
t_c_1_sv int := 0;
t_c_2_sv int := 0;
t_c_3_sv int := 0;
first_tier3_ymto_sv numeric := 0.0;
incidental_bad numeric := 0.0;
incidental_good numeric := 0.0;
lv2a_ymto_1_tlen_sv numeric := 0.0;
gnsis_tlen_sv numeric := 0.0;
pe record;
previous_vews int := 0;
previous_svpth numeric := 0.0;
tier3_ymto_3_tlen_sv numeric := 0.0;
tier3_ymto_tlen_and_complete_sv numeric := 0.0;
tier3_ymto_tlen_sv numeric := 0.0;
tier3_ymto_level_adjuster numeric := 1.0;
cover_tlen_sv numeric := 0.0;
sv numeric := 0;
begin
update thlogepen set svpth = 0;
for pe in
select
e_c_1,
e_c_2,
e_c_3,
t_c_1,
t_c_2,
t_c_3,
t_c_4,
t_c_6,
t_c_7,
tiogol,
vew_count,
first_tier3_ymto,
lv2a_ymto_1,
lv2a_ymto_1_tlen,
gnsis_tlen,
thlogepen_tlen,
thlogepen_id,
tier3_ymto_any,
tier3_ymto_3_tlen,
tier3_ymto_tlen,
tier3_ymto_level,
cover_tlen
from
thlogepen pe
loop
--
-- care technique 1
-- high numbers for this care technique represent very bad mistakes and
-- also significant extra danger for the coindv
--
case
when pe.t_c_1 > 14 then t_c_1_sv := 16;
when pe.t_c_1 > 11 then t_c_1_sv := 13;
when pe.t_c_1 > 8 then t_c_1_sv := 10;
when pe.t_c_1 > 5 then t_c_1_sv := 7;
when pe.t_c_1 > 2 then t_c_1_sv := 5;
when pe.t_c_1 > 0 then t_c_1_sv := 3;
else t_c_1_sv := 0;
end case;
--
-- cover tlen time
-- this has a high impact on the coindv, but it doesn't control it
-- the carer therefore gets higher scores
--
cover_tlen_sv := 0;
case
when pe.cover_tlen > 69 then cover_tlen_sv := 9.5;
when pe.cover_tlen > 49 then cover_tlen_sv := 7.7;
when pe.cover_tlen > 30 then cover_tlen_sv := 5.9;
when pe.cover_tlen > 22 then cover_tlen_sv := 4.4;
when pe.cover_tlen > 15 then cover_tlen_sv := 3;
when pe.cover_tlen > 10 then cover_tlen_sv := 1.9;
when pe.cover_tlen > 8 then cover_tlen_sv := 1.2;
when pe.cover_tlen > 6 then cover_tlen_sv := 0.6;
when pe.cover_tlen > 4 then cover_tlen_sv := 0.2;
else cover_tlen_sv := -2;
end case;
--
-- tlen based sv calculations
-- tlens are multiplied by factors to reflect the ov impact
-- tlens for more significant or severe symptoms have higher multipliers
-- the lv3 tier3 symptom from 1 and 2 is used
--
gnsis_tlen_sv := 0;
lv2a_ymto_1_tlen_sv := 0;
tier3_ymto_3_tlen_sv := 0;
tier3_ymto_tlen_sv := 0;
tier3_ymto_tlen_and_complete_sv := 0;
-- severe levels of tier3 symptom are used to emphasise the time based sv
-- calculations, some exponentiality is added for more severe cases
if
pe.tier3_ymto_level > 2
then
tier3_ymto_level_adjuster := 1
+
(
pe.tier3_ymto_level::float
/
10
);
end if;
-- severities calculated
gnsis_tlen_sv :=
coalesce(pe.thlogepen_tlen, 0)
*
0.1;
lv2a_ymto_1_tlen_sv :=
coalesce(pe.lv2a_ymto_1_tlen, 0)
*
0.2;
tier3_ymto_3_tlen_sv :=
coalesce(pe.tier3_ymto_3_tlen, 0)
*
0.4;
tier3_ymto_tlen_sv :=
coalesce(pe.tier3_ymto_tlen, 0)
*
0.15
*
tier3_ymto_level_adjuster;
tier3_ymto_tlen_and_complete_sv :=
(
coalesce(pe.tier3_ymto_tlen, 0)
-
coalesce(pe.cover_tlen, 0)
)
*
0.7
*
tier3_ymto_level_adjuster;
-- can't be less than 0
if tier3_ymto_tlen_and_complete_sv < 0 then
tier3_ymto_tlen_and_complete_sv := 0;
end if;
--
-- first tier3 symptom (of 1 and 2)
-- earlier is worse
--
case
when pe.first_tier3_ymto < 2 then first_tier3_ymto_sv := 40;
when pe.first_tier3_ymto < 4 then first_tier3_ymto_sv := 35;
when pe.first_tier3_ymto < 6 then first_tier3_ymto_sv := 30;
when pe.first_tier3_ymto < 8 then first_tier3_ymto_sv := 25;
when pe.first_tier3_ymto < 11 then first_tier3_ymto_sv := 20;
when pe.first_tier3_ymto < 14 then first_tier3_ymto_sv := 15;
when pe.first_tier3_ymto < 15 then first_tier3_ymto_sv := 10;
when pe.first_tier3_ymto < 16 then first_tier3_ymto_sv := 8.5;
when pe.first_tier3_ymto < 17 then first_tier3_ymto_sv := 7;
when pe.first_tier3_ymto < 18 then first_tier3_ymto_sv := 5;
when pe.first_tier3_ymto < 20 then first_tier3_ymto_sv := 4;
when pe.first_tier3_ymto < 23 then first_tier3_ymto_sv := 3;
when pe.first_tier3_ymto < 28 then first_tier3_ymto_sv := 2;
when pe.first_tier3_ymto < 50 then first_tier3_ymto_sv := 1;
else first_tier3_ymto_sv := 0;
end case;
if
first_tier3_ymto_sv > 0
then
first_tier3_ymto_sv :=
(
first_tier3_ymto_sv
+
coalesce(pe.vew_count, 0)
)
*
tier3_ymto_level_adjuster;
end if;
--
-- incidental good
--
incidental_good := 0;
if
pe.t_c_3 = 'LPV'
then
incidental_good := incidental_good
+
1;
end if;
if
pe.e_c_2 = true
then
incidental_good := incidental_good
+
3;
end if;
--
-- incidental bad
--
incidental_bad := 0;
if
pe.t_c_3 = 'NTS'
then
incidental_bad := incidental_bad
+
2;
end if;
if
pe.tier3_ymto_any = true
then
incidental_bad := incidental_bad
+
2;
end if;
if
pe.lv2a_ymto_1 > pe.first_tier3_ymto
then
incidental_bad := incidental_bad
+
1;
end if;
if
pe.t_c_7 = true
then
incidental_bad := incidental_bad
+
1;
end if;
if
pe.t_c_6 = true
then
incidental_bad := incidental_bad
+
2;
end if;
if
pe.e_c_3 = false
and
pe.cover_tlen > 2
then
incidental_bad := incidental_bad
+
5;
end if;
if
pe.t_c_4 = false
then
incidental_bad := incidental_bad
+
5;
end if;
--
-- care technique 2
--
t_c_2_sv :=0;
case
when
pe.t_c_2 = 'CMP'
or
pe.t_c_2 = 'CHD'
or
pe.t_c_2 = 'CTP'
then
t_c_2_sv := 2;
when
pe.t_c_2 = 'TIP'
then
t_c_2_sv := 1.5;
when
pe.t_c_2 = 'FFT'
or
pe.t_c_2 = 'KNE'
then
t_c_2_sv := 0.75;
when
pe.t_c_2 = 'SIT'
or
pe.t_c_2 = 'STD'
then
t_c_2_sv := -0.5;
else t_c_2_sv := 0;
end case;
--
-- care equipment 1
--
e_c_1_sv :=0;
case
when
pe.e_c_1 = 'THH'
or
pe.e_c_1 = 'THO'
or
pe.e_c_1 = 'HIH'
or
pe.e_c_1 = 'HIC'
or
pe.e_c_1 = 'ELC'
then
e_c_1_sv := 5;
when
pe.e_c_1 = 'INH'
or
pe.e_c_1 = 'INO'
or
pe.e_c_1 = 'AA2'
then
e_c_1_sv := 2;
when
pe.e_c_1 = 'STR'
or
pe.e_c_1 = 'SCW'
then
e_c_1_sv := -1;
else e_c_1_sv := 0;
end case;
--
-- care technique 3
--
t_c_3_sv :=0;
case
when pe.t_c_3 = 'TID' then t_c_3_sv := 10;
when pe.t_c_3 = 'KNR' then t_c_3_sv := 7;
when pe.t_c_3 = 'RPH' then t_c_3_sv := 2;
when pe.t_c_3 = 'NWI' then t_c_3_sv := 1;
else t_c_3_sv := 0;
end case;
--
-- multiple events are factored in
--
previous_svpth :=0;
previous_vews = coalesce(pe.vew_count, 0);
if
previous_vews > 1
then
previous_svpth =
previous_vews
*
0.5;
end if;
--
-- calculate and update
--
sv =
t_c_1_sv
+
cover_tlen_sv
+
gnsis_tlen_sv
+
lv2a_ymto_1_tlen_sv
+
tier3_ymto_3_tlen_sv
+
tier3_ymto_tlen_and_complete_sv
+
tier3_ymto_tlen_sv
-
incidental_good
+
incidental_bad
+
t_c_2_sv
+
e_c_1_sv
+
t_c_3_sv
+
previous_svpth;
update
thlogepen
set
svpth = sv
where
thlogepen_id = pe.thlogepen_id;
end loop;
return sv;
end;
$$ language plpgsql;