Обсуждение: insert retrieved data into a new table

Поиск
Список
Период
Сортировка

insert retrieved data into a new table

От
e-letter
Дата:
Readers,

Is it possible to use the result of a SELECT function to populate a
new table? The objective is to write the new table for use by gnuplot.

The SELECT function features a mathematical operator such as SUM which
obtains a value. Is it possible to use the value from this function to
be entered into the new table. Please advise the relevant parts of the
documentation to read.

Thanks.

Re: insert retrieved data into a new table

От
Thomas Kellerer
Дата:
e-letter wrote on 24.04.2011 22:52:
> Readers,
>
> Is it possible to use the result of a SELECT function to populate a
> new table? The objective is to write the new table for use by gnuplot.
>
> The SELECT function features a mathematical operator such as SUM which
> obtains a value. Is it possible to use the value from this function to
> be entered into the new table. Please advise the relevant parts of the
> documentation to read.
>

CREATE TABLE new_table
AS
SELECT col1, col2, col3
FROM some_table
WHERE ...

This is the syntax I always use, but I have just realized that it not documented in the manual...


Or using SELECT INTO:
http://www.postgresql.org/docs/current/static/sql-selectinto.html

SELECTcol1, col2, col3
   INTO new_table
FROM some_table
WHERE ...

Thomas

Re: insert retrieved data into a new table

От
Tom Lane
Дата:
e-letter <inpost@gmail.com> writes:
> Is it possible to use the result of a SELECT function to populate a
> new table? The objective is to write the new table for use by gnuplot.

Sure, see INSERT ... SELECT.

            regards, tom lane

Re: insert retrieved data into a new table

От
Gurjeet Singh
Дата:
On Sun, Apr 24, 2011 at 5:13 PM, Thomas Kellerer <spam_eater@gmx.net> wrote:
e-letter wrote on 24.04.2011 22:52:

Readers,

Is it possible to use the result of a SELECT function to populate a
new table? The objective is to write the new table for use by gnuplot.

The SELECT function features a mathematical operator such as SUM which
obtains a value. Is it possible to use the value from this function to
be entered into the new table. Please advise the relevant parts of the
documentation to read.


CREATE TABLE new_table
AS
SELECT col1, col2, col3
FROM some_table
WHERE ...

This is the syntax I always use, but I have just realized that it not documented in the manual...


It sure is:

http://www.postgresql.org/docs/9.0/static/sql-createtableas.html

It should be merged into the CREATE TABLE doc, IMHO.
 

Or using SELECT INTO:
http://www.postgresql.org/docs/current/static/sql-selectinto.html

SELECTcol1, col2, col3
 INTO new_table
FROM some_table
WHERE ...

Thomas



--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice



--
Gurjeet Singh
EnterpriseDB Corporation
The Enterprise PostgreSQL Company

Re: insert retrieved data into a new table

От
Thomas Kellerer
Дата:
Gurjeet Singh wrote on 24.04.2011 23:22:

>     CREATE TABLE new_table
>     AS
>     SELECT col1, col2, col3
>     FROM some_table
>     WHERE ...
>
>     This is the syntax I always use, but I have just realized that it not documented in the manual...
>
>
> It sure is:
>
> http://www.postgresql.org/docs/9.0/static/sql-createtableas.html
>
> It should be merged into the CREATE TABLE doc, IMHO.
>

Ah! Thanks, I was only looking at the CREATE TABLE syntax :)