Re: [SQL] crosstabs

From: daq <daq(at)ugyvitelszolgaltato(dot)hu>
To: "Philippe Lang" <philippe(dot)lang(at)attiksystem(dot)ch>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: [SQL] crosstabs
Date: 2004-02-19 17:23:53
Message-ID: 94113164570.20040219182353@ugyvitelszolgaltato.hu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice pgsql-sql



PL> ------------------------------------------------------------
PL> -- QUERY
PL> ------------------------------------------------------------
PL> SELECT
PL> master_name,
PL> detail_name,
PL> type

PL> FROM
PL> master INNER JOIN detail
PL> ON master.id = detail.id_master

PL> INNER JOIN type
PL> ON detail.code_type = type.code

PL> ORDER by master.id, detail.id;

PL> ------------------------------------------------------------

PL> The result of that is:

PL> ----------------------------------
PL> master_name | detail_name | type |
PL> ----------------------------------
PL> M1 | M1, D1 | TA |
PL> M1 | M1, D2 | TB |
PL> M1 | M1, D3 | TA |
PL> M1 | M1, D4 | TC |
PL> M2 | M2, D1 | TC |
PL> M3 | M3, D1 | TA |
PL> M3 | M3, D2 | TA |
PL> M3 | M3, D3 | TB |
PL> M3 | M3, D4 | TA |
PL> M3 | M3, D5 | TB |
PL> M3 | M3, D6 | TC |
PL> M3 | M3, D7 | TC |
PL> ----------------------------------

PL> I need something like this:

PL> ----------------------------------------
PL> master_name | TA | TB | TC |
PL> ----------------------------------------
PL> M1 | M1, D1 | | |
PL> M1 | | M1, D2 | |
PL> M1 | M1, D3 | | |
PL> M1 | | | M1, D4 |
PL> M2 | | | M2, D1 |
PL> M3 | M3, D1 | | |
PL> M3 | M3, D2 | | |
PL> M3 | | M3, D3 | |
PL> M3 | M3, D4 | | |
PL> M3 | | M3, D5 | |
PL> M3 | | | M3, D6 |
PL> M3 | | | M3, D7 |
PL> ----------------------------------------

PL> Does anyone know how to do that in Postgresql? I run version 7.3.4.

PL> Thanks for any idea you might have.

PL> Philippe Lang

Maybe you can use the CASE construct.

select mastername, case when type='TA' then detail_name else '' end as ta, case .... as tb, case ... as ts from ...

I don't try this, but maybe...

DAQ

In response to

  • crosstabs at 2004-02-19 16:02:10 from Philippe Lang

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Richard Huxton 2004-02-19 17:26:48 Re: crosstabs
Previous Message Philippe Lang 2004-02-19 16:02:10 crosstabs

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2004-02-19 17:26:48 Re: crosstabs
Previous Message Rodrigo Sakai 2004-02-19 17:05:21 Re: Compiling pl/pgsql functions