Problem with pivot tables

From: Gabriel Filipiak <gabriel(dot)filipiak(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Problem with pivot tables
Date: 2011-11-06 17:06:32
Message-ID: CALy5J6WK__RhmnYvtsm0Jnj=aPv+=do32o=fdTk0Zh1V18rCrw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I have problem with creating a pivot table in postgreSQL using crosstab
function. It works well but it produces many records for the same
client_id, how can I avoid it?

Here is the SQL:

SELECT * FROM crosstab('SELECT client_id,extract(year from date),
sum(amount) from orders group by extract(
year from date), client_id','SELECT extract(year from date) FROM orders
GROUP BY extract(year from date) order by extract(year from date)')
AS orders(
row_name integer,
year_2001 text,
year_2002 text,
year_2003 text,
year_2004 text,
year_2005 text,
year_2006 text,
year_2007 text,
year_2008 text,
year_2009 text,
year_2010 text,
year_2011 text);

best regard and thx in advance
Gabe

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Samuel Gendler 2011-11-06 21:26:01 Re: Problem with pivot tables
Previous Message Craig Ringer 2011-11-05 03:35:08 Re: How to implement Aggregate Awareness?