聚合范围
来自 PostgreSQL 维基
跳转到导航跳转到搜索...?? ... 与…相同
SELECT MAX(num_value)-MIN(num_value) AS range_val FROM t;
range()
CREATE OR REPLACE FUNCTION _final_range(numeric[])
RETURNS numeric AS $$
SELECT MAX(val) - MIN(val)
FROM unnest($1) val;
$$ LANGUAGE 'sql' IMMUTABLE;
-- Add aggregate
CREATE AGGREGATE range(numeric) (
SFUNC=array_append, --Function to call for each row. Just builds the array
STYPE=numeric[],
FINALFUNC=_final_range, --Function to call after everything has been added to array
INITCOND='{}' --Initialize an empty array when starting
);
用法
SELECT range(num_value) AS range_val FROM t;
警告
如果您使用的是 PostgreSQL 8.3 或更早版本,则需要添加 数组解包 函数。