@@ -403,20 +403,21 @@ create function spc_intermediates.ewma_step(
403
403
, measurement decimal
404
404
, weighting decimal
405
405
, target_mean decimal
406
+ , scale integer
406
407
) returns decimal immutable language plpgsql as
407
408
$$
408
409
begin
409
410
if last_avg is null then
410
- return weighting * measurement + (1 .0 - weighting) * target_mean;
411
+ return trunc(( weighting * measurement + (1 .0 - weighting) * target_mean), scale) ;
411
412
else
412
- return weighting * measurement + (1 .0 - weighting) * last_avg;
413
+ return trunc(( weighting * measurement + (1 .0 - weighting) * last_avg), scale) ;
413
414
end if;
414
415
end;
415
416
$$;
416
417
417
418
-- The `ewma` aggregate is what wraps up `ewma_step` into an iterative loop. This means it can be used as an aggregate
418
419
-- in the same way as inbuilt aggregates like `sum` or `avg`.
419
- create aggregate spc_intermediates .ewma(measurement decimal , weighting decimal , target_mean decimal ) (
420
+ create aggregate spc_intermediates .ewma(measurement decimal , weighting decimal , target_mean decimal , scale integer ) (
420
421
sfunc = spc_intermediates .ewma_step ,
421
422
stype = decimal
422
423
);
@@ -460,7 +461,8 @@ create function spc_intermediates.ewma_individual_measurements(
460
461
p_weighting decimal ,
461
462
p_limits_width decimal ,
462
463
p_target_mean decimal default null ,
463
- p_target_std_dev decimal default null
464
+ p_target_std_dev decimal default null ,
465
+ scale integer default 8
464
466
) returns table (
465
467
window_id bigint ,
466
468
sample_id bigint ,
@@ -489,7 +491,7 @@ begin
489
491
, wms .period
490
492
, wms .performed_at
491
493
, wms .measured_value
492
- , spc_intermediates .ewma (wms .measured_value , p_weighting, coalesce(p_target_mean, mean_measured_value))
494
+ , spc_intermediates .ewma (wms .measured_value , p_weighting, coalesce(p_target_mean, mean_measured_value), scale )
493
495
over (partition by wms .window_id order by wms .measurement_id ) as ewma
494
496
, coalesce(p_target_mean, mean_measured_value) + (p_limits_width * coalesce(p_target_std_dev, std_dev_measured_value)) *
495
497
sqrt(((p_weighting / (2 - p_weighting)) *
0 commit comments