pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Inserting data in a table using sub-selects
4+ messages / 3 participants
[nested] [flat]

* Inserting data in a table using sub-selects
@ 2004-03-10 19:52 Andreas Joseph Krogh <andreak@officenet.no>
  2004-03-10 20:49 ` Re: Inserting data in a table using sub-selects Stephan Szabo <sszabo@megazone.bigpanda.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Andreas Joseph Krogh @ 2004-03-10 19:52 UTC (permalink / raw)
  To: pgsql-sql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi, I'd like to fill one table with the contents of another table. Mye schema 
is like this:

CREATE TABLE table1(
id serial NOT NULL PRIMARY KEY,
title varchar NOT NULL,
description varchar
);

CREATE TABLE table2(
id int NOT NULL REFERENCES(table1(id) ON DELETE CASCADE,
content varchar NOT NULL
);

Now - is there a way I can do something like:
INSERT INTO table2(id, content) values (select t1.id, coalesce(t1.title, '') 
|| ' ' || coalesce(t1.description, '') as content from table1 t1);

Any hints on how to insert a lot of values at the same time like this?

- -- 
Andreas Joseph Krogh <andreak@officenet.no>
Managing Director, Senior Software Developer
OfficeNet AS

I always do a CVS update before making a patch (unless I forget).

gpg public_key: http://dev.officenet.no/~andreak/public_key.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAT3H1UopImDh2gfQRAsa6AJ9jZjNz25w4iVnxNJYY9LJuG0HBLACfZfup
1TMzQSi1+YYgNjpcampX6wo=
=fJA3
-----END PGP SIGNATURE-----



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Inserting data in a table using sub-selects
  2004-03-10 19:52 Inserting data in a table using sub-selects Andreas Joseph Krogh <andreak@officenet.no>
@ 2004-03-10 20:49 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Stephan Szabo @ 2004-03-10 20:49 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreak@officenet.no>; +Cc: pgsql-sql


On Wed, 10 Mar 2004, Andreas Joseph Krogh wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi, I'd like to fill one table with the contents of another table. Mye schema
> is like this:
>
> CREATE TABLE table1(
> id serial NOT NULL PRIMARY KEY,
> title varchar NOT NULL,
> description varchar
> );
>
> CREATE TABLE table2(
> id int NOT NULL REFERENCES(table1(id) ON DELETE CASCADE,
> content varchar NOT NULL
> );
>
> Now - is there a way I can do something like:
> INSERT INTO table2(id, content) values (select t1.id, coalesce(t1.title, '')
> || ' ' || coalesce(t1.description, '') as content from table1 t1);

I think you mostly just want to remove the values():

Insert into table2(id, content) select t1.id, coalesce(t1.title, '')
 || ' ' || coalesce(t1.description, '') as content from table t1;



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Inserting data in a table using sub-selects]
@ 2004-03-10 20:56 Marty Scholes <marty@outputservices.com>
  2004-03-10 22:33 ` Re: Inserting data in a table using sub-selects] Andreas Joseph Krogh <andreak@officenet.no>
  0 siblings, 1 reply; 4+ messages in thread

From: Marty Scholes @ 2004-03-10 20:56 UTC (permalink / raw)
  To: pgsql-sql

INSERT INTO table2 (id, content) (
    SELECT id, coalesce(title, '') || ' ' || coalesce(description, '')
    FROM table1 t1);

If you want to keep them syncrhonized, in other words, rerun the query
over and over again without having to truncate table2 first or deleting
all of the rows, you can:

INSERT INTO table2 (id, content) (
    SELECT id, coalesce(title, '') || ' ' || coalesce(description, '')
    FROM table1 t1)
WHERE id NOT IN (
    SELECT id
    FROM table1);

With Oracle there is a slick way to do a partial outer join that allowed
you to do this without creating a complete list of table1.id in the last
subquery, but I dunno if Pg has an equivalent mechanism.



Andreas Joseph Krogh wrote:
 > -----BEGIN PGP SIGNED MESSAGE-----
 > Hash: SHA1
 >
 > Hi, I'd like to fill one table with the contents of another table. 
Mye schema
 > is like this:
 >
 > CREATE TABLE table1(
 > id serial NOT NULL PRIMARY KEY,
 > title varchar NOT NULL,
 > description varchar
 > );
 >
 > CREATE TABLE table2(
 > id int NOT NULL REFERENCES(table1(id) ON DELETE CASCADE,
 > content varchar NOT NULL
 > );
 >
 > Now - is there a way I can do something like:
 > INSERT INTO table2(id, content) values (select t1.id, 
coalesce(t1.title, '')
 > || ' ' || coalesce(t1.description, '') as content from table1 t1);
 >
 > Any hints on how to insert a lot of values at the same time like this?
 >
 > - --
 > Andreas Joseph Krogh <andreak@officenet.no>
 > Managing Director, Senior Software Developer
 > OfficeNet AS
 >
 > I always do a CVS update before making a patch (unless I forget).
 >
 > gpg public_key: http://dev.officenet.no/~andreak/public_key.asc
 > -----BEGIN PGP SIGNATURE-----
 > Version: GnuPG v1.2.2 (GNU/Linux)
 >
 > iD8DBQFAT3H1UopImDh2gfQRAsa6AJ9jZjNz25w4iVnxNJYY9LJuG0HBLACfZfup
 > 1TMzQSi1+YYgNjpcampX6wo=
 > =fJA3
 > -----END PGP SIGNATURE-----
 >
 > ---------------------------(end of broadcast)---------------------------
 > TIP 7: don't forget to increase your free space map settings




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Inserting data in a table using sub-selects]
  2004-03-10 20:56 Re: Inserting data in a table using sub-selects] Marty Scholes <marty@outputservices.com>
@ 2004-03-10 22:33 ` Andreas Joseph Krogh <andreak@officenet.no>
  0 siblings, 0 replies; 4+ messages in thread

From: Andreas Joseph Krogh @ 2004-03-10 22:33 UTC (permalink / raw)
  To: Marty Scholes <marty@outputservices.com>; +Cc: pgsql-sql

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday 10 March 2004 21:56, Marty Scholes wrote:
> INSERT INTO table2 (id, content) (
>     SELECT id, coalesce(title, '') || ' ' || coalesce(description, '')
>     FROM table1 t1);
>
> If you want to keep them syncrhonized, in other words, rerun the query
> over and over again without having to truncate table2 first or deleting
> all of the rows, you can:
>
> INSERT INTO table2 (id, content) (
>     SELECT id, coalesce(title, '') || ' ' || coalesce(description, '')
>     FROM table1 t1)
> WHERE id NOT IN (
>     SELECT id
>     FROM table1);
>
> With Oracle there is a slick way to do a partial outer join that allowed
> you to do this without creating a complete list of table1.id in the last
> subquery, but I dunno if Pg has an equivalent mechanism.

Thanks, works perfectly.

- -- 
Andreas Joseph Krogh <andreak@officenet.no>
Managing Director, Senior Software Developer
OfficeNet AS

I always do a CVS update before making a patch (unless I forget).

gpg public_key: http://dev.officenet.no/~andreak/public_key.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAT5fCUopImDh2gfQRArmFAJ4qx31Wx31sehCBo7qfjPwwPWvUzACguw9t
H8mWrl+TsdYIhmQ+FDu41t8=
=qj1H
-----END PGP SIGNATURE-----



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2004-03-10 22:33 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-03-10 19:52 Inserting data in a table using sub-selects Andreas Joseph Krogh <andreak@officenet.no>
2004-03-10 20:49 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2004-03-10 20:56 Re: Inserting data in a table using sub-selects] Marty Scholes <marty@outputservices.com>
2004-03-10 22:33 ` Re: Inserting data in a table using sub-selects] Andreas Joseph Krogh <andreak@officenet.no>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox