Percentile IQR Fences Flag Outliers

Take per-group Q1 and Q3 with percentile_cont in a CTE, derive the 1.5x IQR fences, flag rows outside them, and count outliers per group.

SqlData Integrity

Code

snippet
CREATE TABLE api_calls (
    call_id    int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    region     text NOT NULL,
    latency_ms int  NOT NULL
);

INSERT INTO api_calls (region, latency_ms) VALUES
    ('eu', 40), ('eu', 52), ('eu', 61), ('eu', 65), ('eu', 300),
    ('us', 18), ('us', 22), ('us', 25), ('us', 30), ('us', 31),
    ('ap', 95), ('ap', 101), ('ap', 108), ('ap', 120), ('ap', 140);

-- 有序集聚合(percentile_cont)不能当窗口函数用:先在 CTE 里按组算分位
WITH fences AS (
    SELECT region,
           percentile_cont(0.25) WITHIN GROUP (ORDER BY latency_ms) AS q1,
           percentile_cont(0.75) WITHIN GROUP (ORDER BY latency_ms) AS q3,
           1.5 * (percentile_cont(0.75) WITHIN GROUP (ORDER BY latency_ms)
                - percentile_cont(0.25) WITHIN GROUP (ORDER BY latency_ms)) AS iqr_spread
    FROM api_calls
    GROUP BY region
),
flagged AS (
    SELECT c.call_id, c.region, c.latency_ms,
           c.latency_ms < f.q1 - f.iqr_spread
               OR c.latency_ms > f.q3 + f.iqr_spread AS outlier
    FROM api_calls c
    JOIN fences f USING (region)
)
SELECT region, call_id, latency_ms, outlier
FROM flagged
ORDER BY region, latency_ms DESC;

-- 每组围栏外计数:eu 抓到 300,其余组为 0
-- CTE 只在单条语句内可见:聚合语句要自带 WITH,不能引用上一条语句的 flagged
WITH fences AS (
    SELECT region,
           percentile_cont(0.25) WITHIN GROUP (ORDER BY latency_ms) AS q1,
           percentile_cont(0.75) WITHIN GROUP (ORDER BY latency_ms) AS q3,
           1.5 * (percentile_cont(0.75) WITHIN GROUP (ORDER BY latency_ms)
                - percentile_cont(0.25) WITHIN GROUP (ORDER BY latency_ms)) AS iqr_spread
    FROM api_calls
    GROUP BY region
),
flagged AS (
    SELECT c.call_id, c.region, c.latency_ms,
           c.latency_ms < f.q1 - f.iqr_spread
               OR c.latency_ms > f.q3 + f.iqr_spread AS outlier
    FROM api_calls c
    JOIN fences f USING (region)
)
SELECT region, COUNT(*) FILTER (WHERE outlier) AS outliers
FROM flagged
GROUP BY region
ORDER BY region;

How the snippet works

SELECT / INSERT / CREATE statements share the script, touching api_calls, fences and flagged.

Breaking the statements into their working parts: a CTE (WITH … AS) that names an intermediate result before the main query; 2 JOINs combining rows across tables; a WHERE clause narrowing which rows one statement affects; COUNT aggregation; a GROUP BY that collapses matching rows into one output row per group; an ORDER BY that fixes the output order; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; a GENERATED ALWAYS/GENERATED BY clause that computes the column value.

Watch out for

  • 2 joins multiply the intermediate row count; index the join keys before running this shape against full-size tables.

Using it

Run the statement against the api_calls table.

Key statements

INSERT INTO api_calls (region, latency_ms) VALUES
    ('eu', 40), ('eu', 52), ('eu', 61), ('eu', 65), ('eu', 300),
    ('us', 18), ('us', 22), ('us', 25), ('us', 30), ('us', 31),
    ('ap', 95), ('ap', 101), ('ap', 108), ('ap', 120), ('ap', 140);
CREATE TABLE api_calls (
    call_id    int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    region     text NOT NULL,
    latency_ms int  NOT NULL
);

Interactive Tools for This Topic

FAQ

Which objects does the SQL touch?
`api_calls`, `fences`, `flagged` — joined via 2 JOIN clauses.