Assuming a table like this:
CREATE TABLE t (
link_ids int[] PRIMARY KEY -- which is odd for a PK
, length int[]
, CHECK (length <> '{}'::int[]) -- rules out null and empty in length
);
This query works in Postgres 9.3+
SELECT link_ids, min(len) As min_length
FROM t, unnest(t.length) len -- implicit LATERAL join
GROUP BY 1;
Or you create a little function (Postgres 8.4+):
CREATE OR REPLACE FUNCTION arr_min(anyarray) RETURNS anyelement AS
'SELECT min(i) FROM unnest($1) i' LANGUAGE sql IMMUTABLE;
Then:
SELECT link_ids, arr_min(length) AS min_length FROM t;
Or, to make this fast, as long as we are dealing with integer
arrays of trivial length, you could use the additional module intarray and use the built-in sort()
function (Postgres 8.3+):
SELECT link_ids, (sort(length))[1] AS min_length FROM t;
link_ids
unique? Are arrays in ascending order like your example suggests? If you are working with an actual table, post the table definition. Else, it would be better to post your recursive query: there might be a better solution to begin with. (Plus table definitions for underlying tables.) – Erwin Brandstetter Feb 10 at 0:37