pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Henry Lafleur <HLafleur@phoenixforge.com>
To: Volker Paul <vpaul@dohle.com>
To: Dana.Reed@clinicaldatacare.com
Cc: pgsql-sql@postgresql.org
Subject: RE: SQL (table transposition)
Date: Fri, 4 Aug 2000 08:57:14 -0400
Message-ID: <E332B20358CDD1118D7A00A0C995F75A914730@XSERVER> (raw)

Hi,

When I saw cross tab, I realized that I'd done this before. 

If you know what your keys are ahead of time, you can write the query.
Otherwise, you can write a program go generate the query by looking at the
distinct list of keys and generating code as follows. The code generator
would only have to replace the ?'s in "SUM(CASE KEY WHEN ? THEN VALUE ELSE 0
END) AS KEY?VAL".

SELECT INDEX AS T1_INDEX, SUM(CASE KEY WHEN 1 THEN VALUE ELSE 0 END) AS
KEY1VAL, 
	SUM(CASE KEY WHEN 2 THEN VALUE ELSE 0 END) AS KEY2VAL, 
	SUM(CASE KEY WHEN 3 THEN VALUE ELSE 0 END) AS KEY3VAL
FROM T1, T2
WHERE T1.INDEX = T2.IND
GROUP BY T1.INDEX ;

Henry


-----Original Message-----
From: Volker Paul [mailto:vpaul@dohle.com]
Sent: Friday, August 04, 2000 2:30 AM
To: Dana.Reed@clinicaldatacare.com
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] SQL (table transposition)


Dana.Reed@clinicaldatacare.com wrote:
> 
> Is there any way to get table T3 (below) from T1 and T2 using SQL (select,
view, etc)?
> 
> T3 is basically all INDEX values from T1 matched to IND from T2 with the
corresponding KEY/VALUE pairs transposed from rows to columns.
> 
> -------
> |INDEX|   (T1)
> -------
> |  1  |
> |  2  |
> |  3  |
> -------
> 
> -----------------
> |IND|KEY| VALUE |   (T2)
> -----------------
> | 1 | 1 | val_a |
> | 1 | 2 | val_b |
> | 1 | 3 | val_c |
> | 2 | 1 | val_d |
> | 2 | 2 | val_e |
> | 3 | 1 | val_f |
> | 3 | 3 | val_g |
> -----------------
> 
> ----------------------------------
> |T1_INDEX|KEY1VAL|KEY2VAL|KEY3VAL|   (T3)
> ----------------------------------
> |   1    | val_a | val_b | val_c |
> |   2    | val_d | val_e |       |
> |   3    | val_f |       | val_g |
> ----------------------------------
> 


I think what you are looking for is cross tabulation, 
TRANSFORM statement, but I don't know if that
is supported by PostgreSQL.

Volker Paul



view thread (4+ messages)

Message-ID: <E332B20358CDD1118D7A00A0C995F75A914730@XSERVER>
Permalink:  ../E332B20358CDD1118D7A00A0C995F75A914730@XSERVER/
Also on:    postgresql.org/message-id/E332B20358CDD1118D7A00A0C995F75A914730@XSERVER

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: HLafleur@phoenixforge.com, vpaul@dohle.com, Dana.Reed@clinicaldatacare.com
  Subject: Re: SQL (table transposition)
  In-Reply-To: <E332B20358CDD1118D7A00A0C995F75A914730@XSERVER>
  References: <E332B20358CDD1118D7A00A0C995F75A914730@XSERVER>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox