Re: crosstab de tablefunc no da los datos deseados

From: "Jaime Casanova" <systemguards(at)gmail(dot)com>
To: "Ignacio Ayala Leal" <ignacioal_3(at)hotmail(dot)com>
Cc: pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: crosstab de tablefunc no da los datos deseados
Date: 2007-02-23 04:19:35
Message-ID: c2d9e70e0702222019n339376a8w18aaaca00276e0c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

On 2/22/07, Ignacio Ayala Leal <ignacioal_3(at)hotmail(dot)com> wrote:
> CREATE OR REPLACE FUNCTION crosstab_reporte(text) RETURNS SETOF meses AS
> '$libdir/tablefunc', 'crosstab' LANGUAGE 'c' STABLE STRICT;
>
> hago la consulta select * from crosstab_reporte('select cliente,mes,importe
> from cte order by 1,2;')
>

como tu sabes el numero de columnas (categorias, en este caso meses)
que van a haber puedes hacer la consulta asi para evitar usar
crosstab:

select cliente,
sum(case mes when 1 then importe else 0 end) as enero,
sum(case mes when 2 then importe else 0 end) as febrero,
sum(case mes when 3 then importe else 0 end) as marzo,
sum(case mes when 4 then importe else 0 end) as abril,
sum(case mes when 5 then importe else 0 end) as mayo,
sum(case mes when 6 then importe else 0 end) as junio,
sum(case mes when 7 then importe else 0 end) as julio,
sum(case mes when 8 then importe else 0 end) as agosto,
sum(case mes when 9 then importe else 0 end) as septiembre,
sum(case mes when 10 then importe else 0 end) as octubre,
sum(case mes when 11 then importe else 0 end) as noviembre,
sum(case mes when 12 then importe else 0 end) as diciembre
from cte
group by cliente
order by cliente;

--
Atentamente,
Jaime Casanova

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs and the universe trying
to produce bigger and better idiots.
So far, the universe is winning."
Richard Cook

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Jaime Casanova 2007-02-23 04:24:32 Re: Necesito hacer un instalador
Previous Message Alvaro Herrera 2007-02-23 03:37:53 Re: Conectar C# con Postgres