dbTalk Databases Forums  

Turning a subselect into an array

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Turning a subselect into an array in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jim C. Nasby
 
Posts: n/a

Default Turning a subselect into an array - 10-28-2004 , 05:37 PM






I'm sure this has been answered before, but the search seems to be down
again.

How can I convert the results of a subselect into an array? IE:

CREATE TABLE a(a int, b int, c int[]);
INSERT INTO table_a
SELECT a, b, (SELECT c FROM table_c WHERE table_c.parent = table_b.id)
FROM table_b
;
--
Jim C. Nasby, Database Consultant decibel (AT) decibel (DOT) org
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

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


Reply With Quote
  #2  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: Turning a subselect into an array - 10-28-2004 , 06:21 PM






On Thu, Oct 28, 2004 at 05:37:29PM -0500, Jim C. Nasby wrote:
Quote:
I'm sure this has been answered before, but the search seems to be down
again.

How can I convert the results of a subselect into an array? IE:

CREATE TABLE a(a int, b int, c int[]);
INSERT INTO table_a
SELECT a, b, (SELECT c FROM table_c WHERE table_c.parent = table_b.id)
FROM table_b
See the "Array Constructors" section in the PostgreSQL documentation:

http://www.postgresql.org/docs/7.4/s...Y-CONSTRUCTORS

INSERT INTO table_a
SELECT a, b, ARRAY(SELECT c FROM table_c WHERE table_c.parent = table_b.id)
FROM table_b

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

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



Reply With Quote
  #3  
Old   
Jim C. Nasby
 
Posts: n/a

Default Re: Turning a subselect into an array - 10-29-2004 , 05:13 PM



Ok, next stupid question that I can't find in the docs... How would I
join a table to an array? IE: if I have an array of primary keys for
some table and I want to get the name field from that table and turn it
back into an array, how would I do that?

On Thu, Oct 28, 2004 at 05:21:52PM -0600, Michael Fuhr wrote:
Quote:
On Thu, Oct 28, 2004 at 05:37:29PM -0500, Jim C. Nasby wrote:
I'm sure this has been answered before, but the search seems to be down
again.

How can I convert the results of a subselect into an array? IE:

CREATE TABLE a(a int, b int, c int[]);
INSERT INTO table_a
SELECT a, b, (SELECT c FROM table_c WHERE table_c.parent = table_b.id)
FROM table_b

See the "Array Constructors" section in the PostgreSQL documentation:

http://www.postgresql.org/docs/7.4/s...Y-CONSTRUCTORS

INSERT INTO table_a
SELECT a, b, ARRAY(SELECT c FROM table_c WHERE table_c.parent = table_b.id)
FROM table_b

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

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

--
Jim C. Nasby, Database Consultant decibel (AT) decibel (DOT) org
Give your computer some brain candy! www.distributed.net Team #1828

Windows: "Where do you want to go today?"
Linux: "Where do you want to go tomorrow?"
FreeBSD: "Are you guys coming, or what?"

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #4  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: Turning a subselect into an array - 10-29-2004 , 11:40 PM



On Fri, Oct 29, 2004 at 05:13:02PM -0500, Jim C. Nasby wrote:
Quote:
Ok, next stupid question that I can't find in the docs... How would I
join a table to an array? IE: if I have an array of primary keys for
some table and I want to get the name field from that table and turn it
back into an array, how would I do that?
See the "Subquery Expressions" and "Row and Array Comparisons"
sections in the PostgreSQL documentation:

http://www.postgresql.org/docs/7.4/s...-subquery.html
http://www.postgresql.org/docs/7.4/s...mparisons.html

If I understand you correctly, this should work:

SELECT ARRAY(SELECT name FROM foo WHERE id = ANY(ARRAY[1,2,3]));

A test I just ran showed "id = ANY(ARRAY[1,2,3])" doing a sequential
scan whereas "id IN (1,2,3)" did an index scan, so you might want to
use the latter if possible.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.