Re: create table as problem - Mailing list pgsql-general

From Michael Fuhr
Subject Re: create table as problem
Date
Msg-id 20060915210047.GA33331@winnie.fuhr.org
Whole thread Raw
In response to create table as problem  (garry saddington <garry@schoolteachers.co.uk>)
List pgsql-general
On Fri, Sep 15, 2006 at 09:42:35PM +0100, garry saddington wrote:
> I am getting a syntax error at or near 'as' in this method, can anyone
> help?
>
> create table iclasses
> (classid serial,
> subject text,
> year text,
> groups text,
> teacher text,
> set text
> )
> as select distinct subject,year,groups,teacher,set from interimclasses

With CREATE TABLE AS you can specify column names but not column
types.  If you want to create more columns than the query returns
then try an ordinary CREATE TABLE followed by INSERT INTO ... SELECT.

CREATE TABLE iclasses (
    classid  serial,
    subject  text,
    year     text,
    groups   text,
    teacher  text,
    set      text
);

INSERT INTO iclasses (subject, year, groups, teacher, set)
  SELECT DISTINCT subject, year, groups, teacher, set
  FROM interimclasses;

--
Michael Fuhr

pgsql-general by date:

Previous
From: garry saddington
Date:
Subject: create table as problem
Next
From: Poul Jensen
Date:
Subject: Re: ECPG: non-integer constant in group by