Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created June 19, 2019 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertsosinski/d4ffafe33ce100e675b9431f63c37b5d to your computer and use it in GitHub Desktop.
Save robertsosinski/d4ffafe33ce100e675b9431f63c37b5d to your computer and use it in GitHub Desktop.
Calculating out-of-bounds values using standard deviation
with nums as (
select * from (values (18), (3), (3), (2), (4), (2), (5), (3), (12), (0.00003), (4), (2)) as n (x)
), a as (
select
stddev(nums.x) as sdev,
abs(avg(nums.x)) as aavg,
numrange(
(abs(avg(nums.x) - stddev(nums.x))),
(abs(avg(nums.x) + stddev(nums.x)))
) as bounds
from nums
)
select nums.x, a.aavg, a.sdev, a.bounds, a.bounds @> nums.x::numeric from nums, a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment