Re: How to count elements in an array?
Re: How to count elements in an array?
От:
Josh Berkus <josh@agliodbs.com>
Дата:
Grant, The only way I know to enumarate arrays is procedural. This is a good reason to stay away from arrays except in buffer and temporary tables (aside from the fact that array columns violate the relational principle of atomicity). The following sample procedure, paraphrased from Bruce Momjian's book (which you should buy, hint, hint) illustrates enumerating an array of INT4 values: CREATE FUNCTION count_array ( INT4[] ) RETURNS INT2 AS ' DECLARE array_loop INT2;array_col ALIAS FOR $1; BEGINarray_loop := 1;WHILE array_col[array_loop] LOOP array_loop = array_loop + 1;END LOOP;array_loop := array_loop - 1RETURN array_loop; END;' LANGUAGE 'plpgsql'; -Josh -- ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete information technology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco
How to count elements in an array?
От:
Grant <grant@conprojan.com.au>
Дата:
How can I obtain a count (number) of elements in a text array? Thanks.
test=# \d array Table "array" Attribute | Type | Modifier
--------------+---------+------------------------------------------------id | integer | not null default nextval('array_id_seq'::text)to_group_ids | text[] |
Index: array_pkey
test=#
--- EXAMPLE SELECT ---
test=# select to_group_ids[1] from array where id=1;to_group_ids
--------------1
(1 row)
test=#