473,468 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how can I select into an array?

I would like to select strings from a table and return them as an array
For example,
select new_array(name) from my_tbl
would return
String[] { name1, name2, name3, etc }

Is this possible with built-in SQL/psql functions?
If not, how hard would it be to write a function that does this? (given that
I have coding experience but none writing pgsql functions)

Andy Kriger | Software Mechanic | Greater Than One, Inc.
28 West 27th Street | 7th Floor | New York, NY 10001
P: 212.252.7197 | F: 212.252.7364 | E: ak*****@greaterthanone.com
Nov 22 '05 #1
8 31228
hello,

try:

CREATE OR REPLACE FUNCTION aggregate_array(ANYARRAY,ANYELEMENT) RETURNS
ANYARRAY AS '
SELECT
CASE
WHEN $1 IS NULL
THEN ARRAY[$2]
WHEN $2 IS NULL
THEN $1
ELSE array_append($1,$2)
END;
' LANGUAGE 'SQL';

CREATE AGGREGATE aggarray (BASETYPE = ANYELEMENT, SFUNC = aggregate_array,
STYPE = ANYARRAY);

or

CREATE AGGREGATE aggarray (basetype = anyelement, sfunc = array_append,
stype = anyarray, initcond = '{}' );

testdb011=> SELECT count(*), aggarray(prijmeni) FROM lide GROUP BY
prijmeni ~ '.*á';

regards
Pavel Stehule


On Fri, 6 Feb 2004, Andy Kriger wrote:
I would like to select strings from a table and return them as an array
For example,
select new_array(name) from my_tbl
would return
String[] { name1, name2, name3, etc }

Is this possible with built-in SQL/psql functions?
If not, how hard would it be to write a function that does this? (given that
I have coding experience but none writing pgsql functions)

Andy Kriger | Software Mechanic | Greater Than One, Inc.
28 West 27th Street | 7th Floor | New York, NY 10001
P: 212.252.7197 | F: 212.252.7364 | E: ak*****@greaterthanone.com

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #2
Thank you for your response - I should have mention I'm using Postgres 7.2.x
ANYARRAY does not appear to exist in that version
Is there a workaround?

-----Original Message-----
From: Pavel Stehule [mailto:st*****@kix.fsv.cvut.cz]
Sent: Friday, February 06, 2004 10:49 AM
To: Andy Kriger
Cc: Pgsql-General
Subject: Re: [GENERAL] how can I select into an array?

hello,

try:

CREATE OR REPLACE FUNCTION aggregate_array(ANYARRAY,ANYELEMENT) RETURNS
ANYARRAY AS '
SELECT
CASE
WHEN $1 IS NULL
THEN ARRAY[$2]
WHEN $2 IS NULL
THEN $1
ELSE array_append($1,$2)
END;
' LANGUAGE 'SQL';

CREATE AGGREGATE aggarray (BASETYPE = ANYELEMENT, SFUNC = aggregate_array,
STYPE = ANYARRAY);

or

CREATE AGGREGATE aggarray (basetype = anyelement, sfunc = array_append,
stype = anyarray, initcond = '{}' );

testdb011=> SELECT count(*), aggarray(prijmeni) FROM lide GROUP BY prijmeni
~ '.*á';

regards
Pavel Stehule


On Fri, 6 Feb 2004, Andy Kriger wrote:
I would like to select strings from a table and return them as an
array For example, select new_array(name) from my_tbl would return
String[] { name1, name2, name3, etc }

Is this possible with built-in SQL/psql functions?
If not, how hard would it be to write a function that does this?
(given that I have coding experience but none writing pgsql functions)

Andy Kriger | Software Mechanic | Greater Than One, Inc.
28 West 27th Street | 7th Floor | New York, NY 10001
P: 212.252.7197 | F: 212.252.7364 | E: ak*****@greaterthanone.com


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #3
if you can in plpgsql 7.2.x return array of known type, you can replace
anyarray and anyelement like varchar[], varchar. But I don't know if it
7.2 supported.

Pavel

On Fri, 6 Feb 2004, Andy Kriger wrote:
Thank you for your response - I should have mention I'm using Postgres 7.2.x
ANYARRAY does not appear to exist in that version
Is there a workaround?

-----Original Message-----
From: Pavel Stehule [mailto:st*****@kix.fsv.cvut.cz]
Sent: Friday, February 06, 2004 10:49 AM
To: Andy Kriger
Cc: Pgsql-General
Subject: Re: [GENERAL] how can I select into an array?

hello,

try:

CREATE OR REPLACE FUNCTION aggregate_array(ANYARRAY,ANYELEMENT) RETURNS
ANYARRAY AS '
SELECT
CASE
WHEN $1 IS NULL
THEN ARRAY[$2]
WHEN $2 IS NULL
THEN $1
ELSE array_append($1,$2)
END;
' LANGUAGE 'SQL';

CREATE AGGREGATE aggarray (BASETYPE = ANYELEMENT, SFUNC = aggregate_array,
STYPE = ANYARRAY);

or

CREATE AGGREGATE aggarray (basetype = anyelement, sfunc = array_append,
stype = anyarray, initcond = '{}' );

testdb011=> SELECT count(*), aggarray(prijmeni) FROM lide GROUP BY prijmeni
~ '.*á';

regards
Pavel Stehule


On Fri, 6 Feb 2004, Andy Kriger wrote:
I would like to select strings from a table and return them as an
array For example, select new_array(name) from my_tbl would return
String[] { name1, name2, name3, etc }

Is this possible with built-in SQL/psql functions?
If not, how hard would it be to write a function that does this?
(given that I have coding experience but none writing pgsql functions)

Andy Kriger | Software Mechanic | Greater Than One, Inc.
28 West 27th Street | 7th Floor | New York, NY 10001
P: 212.252.7197 | F: 212.252.7364 | E: ak*****@greaterthanone.com

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 22 '05 #4
"Andy Kriger" <ak*****@greaterthanone.com> writes:
I would like to select strings from a table and return them as an array


You can do that beginning in 7.4 with the ARRAY(sub-select) construct.

regression=# select f1 from text_tbl;
f1
-------------------
doh!
hi de ho neighbor
(2 rows)

regression=# select array(select f1 from text_tbl);
?column?
----------------------------
{doh!,"hi de ho neighbor"}
(1 row)

regression=#

In prior versions you could probably fake it with a loop in a plpgsql
function, but it'd be kinda awkward.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #5
Pavel Stehule wrote:
CREATE AGGREGATE aggarray (BASETYPE = ANYELEMENT, SFUNC = aggregate_array,
STYPE = ANYARRAY);


Or, from the docs, see:
http://www.postgresql.org/docs/curre...tic/xaggr.html

CREATE AGGREGATE array_accum (
sfunc = array_append,
basetype = anyelement,
stype = anyarray,
initcond = '{}'
);

array_append() is built-in in 7.4 -- and note both Pavel's solution and
this one require 7.4.x

HTH,

Joe

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #6
Pavel Stehule wrote:
if you can in plpgsql 7.2.x return array of known type, you can replace
anyarray and anyelement like varchar[], varchar. But I don't know if it
7.2 supported.


PL/pgSQL array support in anything earlier than 7.4 is pretty weak. I
would strongly recommend upgrading to 7.4 if arrays are important to you.

Joe

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 22 '05 #7
this is a followup to my original message for anyone else stuck on older
psql versions...

i wrote a function to accomplish selecting into an array which works in
7.2.1
you will need to 'createlang plpgsql' (see the docs on procedural languages)
before you can use it
it takes a SQL query as an arg and returns an array aggregated from the
result column named 'arrayval'
it is written to work with varchars but could easily be modified to work
with other data types

-- create an array from the results of a query
-- query must return a column named arrayval
-- $1 = query string
CREATE OR REPLACE FUNCTION array_query(VARCHAR)
RETURNS VARCHAR[]
AS '
DECLARE
query ALIAS FOR $1;
rec RECORD;
str VARCHAR;
arr VARCHAR[];
BEGIN
str := ''{'';
FOR rec IN EXECUTE query LOOP
str := str || ''"'' || rec.arrayval || ''"'' || '','';
END LOOP;
str := str || ''}'';
SELECT INTO arr str;
RETURN arr;
END;
' LANGUAGE 'plpgsql';
_____

From: pg*****************@postgresql.org
[mailto:pg*****************@postgresql.org] On Behalf Of Andy Kriger
Sent: Friday, February 06, 2004 10:37 AM
To: Pgsql-General
Subject: [GENERAL] how can I select into an array?

I would like to select strings from a table and return them as an array
For example,
select new_array(name) from my_tbl
would return
String[] { name1, name2, name3, etc }

Is this possible with built-in SQL/psql functions?
If not, how hard would it be to write a function that does this? (given that
I have coding experience but none writing pgsql functions)

Andy Kriger | Software Mechanic | Greater Than One, Inc.
28 West 27th Street | 7th Floor | New York, NY 10001
P: 212.252.7197 | F: 212.252.7364 | E: ak*****@greaterthanone.com
Nov 22 '05 #8

On Fri, Feb 06, 2004 at 12:08:16PM -0500, Tom Lane wrote:
"Andy Kriger" <ak*****@greaterthanone.com> writes:
I would like to select strings from a table and return them as an array


You can do that beginning in 7.4 with the ARRAY(sub-select) construct.

regression=# select f1 from text_tbl;
f1
-------------------
doh!
hi de ho neighbor
(2 rows)

regression=# select array(select f1 from text_tbl);
?column?
----------------------------
{doh!,"hi de ho neighbor"}
(1 row)

regression=#

In prior versions you could probably fake it with a loop in a plpgsql
function, but it'd be kinda awkward.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Joe Conway supplied a 7.3 version of function that
could help you do this. I have not tested it in
7.2, however. The details of this function for
getting the next array index for appending to the
array are written up in http://www.varlena.com/GeneralBits/24.html

CREATE OR REPLACE FUNCTION array_next(text[]) returns int AS '
DECLARE
arr alias for $1;
high int;
BEGIN
high := 1 +
replace(split_part(array_dims(arr),'':'',2),'']'','''')::int;
RETURN high;
END;
' LANGUAGE 'plpgsql' IMMUTABLE STRICT;

create table mytable (myarray text[]);
insert into mytable values ('{"abc","d e f"}');
update mytable set myarray[array_next(myarray)] = 'new element';
regression=# select * from mytable;
myarray
-----------------------------
{abc,"d e f","new element"}
(1 row)

Elein

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: point | last post by:
Hello there... I'm a PHP programmer and starting to learn JS... I have a following problem.... I have 3 select boxes! one is hotel one is destination and one is country... if someone...
3
by: Stewart | last post by:
Dear comp.lang.javascript, I have more than once wanted to manipulate the contents of select boxes dynamically, whilst the boxes contain <optgroup> tags. Manipulation of a select box containing...
1
by: supline | last post by:
I have a function that creates a select list: function make_select_ns($name,$array,$persist){ $string = "<select name=\"$name\">"; $string .="<option selected>" . ucfirst($name) . "</option>\n";...
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
4
by: Matt Ratliff | last post by:
Hello, I would appreciate any assistance you have with the following problem: I have (as an example) an array of values as follows: arrayvalues=new Array("0001","0003","0005") where each is the...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
6
by: cathyisfrustrated | last post by:
Hey, I've been trying to learn javascript for a few days. I'm a bit stuck! I'd appreciate any links to get me going in the right direction or any advice I can get. I've written a...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
4
tjc0ol
by: tjc0ol | last post by:
Hi guys, I'm a newbie in php and I got error in my index.php which is: 1054 - Unknown column 'p.products_id' in 'on clause' select p.products_image, pd.products_name, p.products_id,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.