Skip to content

Instantly share code, notes, and snippets.

View mmatiaschek's full-sized avatar

Markus Matiaschek mmatiaschek

View GitHub Profile
@mmatiaschek
mmatiaschek / example.sql
Created July 26, 2019 20:20 — forked from wolever/example.sql
A simple Postgres aggregate function for calculating a trimmed mean, excluding values outside N standard deviations from the mean: `tmean(v, standard_deviations)` (for example: `tmean(rating, 1.75)`).
DROP TABLE IF EXISTS foo;
CREATE TEMPORARY TABLE foo (x FLOAT);
INSERT INTO foo VALUES (1);
INSERT INTO foo VALUES (2);
INSERT INTO foo VALUES (3);
INSERT INTO foo VALUES (4);
INSERT INTO foo VALUES (100);
SELECT avg(x), tmean(x, 2.0), tmean(x, 1.5) FROM foo;