Re: Data format and display

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: Josué Maldonado <josue(at)lamundial(dot)hn>, pgsql general list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Data format and display
Date: 2005-01-20 23:20:31
Message-ID: 200501201520.31231.scrawford@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> I have a table that contains this raw data:
>
> epr_procode | epr_tipo | epr_mes | epr_valor | zert_title
> -------------+----------+---------+-----------+--------------------
>-- 00C188 | VTA | 200309 | 2116.0000 | Venta
> 00C188 | CTO | 200309 | 1600.0700 | Costo
> 00C188 | VTA | 200311 | 3450.0000 | Venta
> 00C188 | CTO | 200311 | 2687.4200 | Costo
>
> I need to display it this way:
>
> TITULO |200309 |200310 |200311 |200312
> -------------+----------+---------+-----------+--------------------
>-- Venta |2116.0000 |0.0000 |3450.0000 |0.0000
> Costo |1600.0700 |0.0000 |2687.4200 |0.0000
>
>
> Notice I must display the missing 200310 and 200312 empty data
> since users retrieves the info in four months based data set, of
> course that data does not exist since there were no trans in those
> months. Any idea or suggestion to get it done in Postgresql.

Here's one way:

select
zert_title as TITULO,
sum(case when epr_mes=200309 then epr_valor else 0 end) as "200309",
sum(case when epr_mes=200310 then epr_valor else 0 end) as "200310",
sum(case when epr_mes=200311 then epr_valor else 0 end) as "200311",
sum(case when epr_mes=200312 then epr_valor else 0 end) as "200312"
group by 1;

Cheers,
Steve

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Oluwatope Akinniyi 2005-01-20 23:30:39 PLPHP
Previous Message Josué Maldonado 2005-01-20 23:19:21 Re: Data format and display