group sum function copy csv failure

From: e-letter <inpost(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: group sum function copy csv failure
Date: 2013-09-13 17:01:22
Message-ID: CAET1fe61Xnhj-aobtABQQePxPkWwagj69KiDQC+s1tCrtj330A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Readers,

A table was created to perform a sum function on a group of rows:

create table testgroupsum (itemname varchar(50),date date,amount numeric);
CREATE TABLE
set datestyle to DMY;
SET
insert into testgroupsum (itemname,date,amount) values ('item
1','2013-01-01','100');
INSERT 0 1
insert into testgroupsum (itemname,date,amount) values ('item
2','2013-01-10','35');
INSERT 0 1
insert into testgroupsum (itemname,date,amount) values ('item
3','2013-02-20','50');
INSERT 0 1
insert into testgroupsum (itemname,date,amount) values ('item
a','2013-01-25','-500');
INSERT 0 1
insert into testgroupsum (itemname,date,amount) values ('item
b','2013-02-28','-20');
INSERT 0 1
copy testgroupsum (SELECT EXTRACT(month FROM(date)), SUM(amount) FROM
testgroupsum GROUP
(> BY EXTRACT(month FROM(date))) to /home/r/tmp/data.csv;
ERROR: syntax error at or near "SELECT"
LINE 1: copy testgroupsum (SELECT EXTRACT(month FROM(date)), SUM(amo...
^
\copy testgroupsum (SELECT EXTRACT(month FROM(date)), SUM(amount) FROM
testgroupsum GROUP
BY EXTRACT(month FROM(date))) to /local/path/to/data.csv;
\copy: parse error at ")"
SELECT EXTRACT(month FROM(date)), SUM(amount) FROM testgroupsum GROUP
BY EXTRACT(month FROM(date));
date_part | sum
-----------+------
1 | -365
2 | 30
(2 rows)

What is the cause of the error, preventing export of the query as a csv file?

Browse pgsql-novice by date

  From Date Subject
Next Message Albe Laurenz 2013-09-13 18:37:47 Re: PostgreSQL 9.3 Error when trying to run Create Extension postgis
Previous Message DFE 2013-09-13 15:02:39 PostgreSQL 9.3 Error when trying to run Create Extension postgis