pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mark Volpe <volpe.mark@epamail.epa.gov>
To: Dana.Reed@clinicaldatacare.com
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL (table transposition)
Date: Thu, 03 Aug 2000 11:46:27 -0400
Message-ID: <398993D3.D421AAB7@epamail.epa.gov> (raw)
References: <TFSUYCTU@clinicaldatacare.com>

Hope you like black magic :)

SELECT IND AS T1_INDEX, MIN(CASE WHEN KEY=1 THEN VALUE ELSE NULL END) AS
KEY1VAL, MIN(CASE WHEN KEY=2 THEN VALUE ELSE NULL END) AS KEY2VAL, MIN(CASE
WHEN KEY=3 THEN VALUE ELSE NULL END) AS KEY3VAL FROM T2 GROUP BY IND ORDER BY
IND;

Mark

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 |
> ----------------------------------
> 
> Thanks for any suggestions
> 
> med vänlig hälsning
> /Dana



view thread (4+ messages)  latest in thread

Message-ID: <398993D3.D421AAB7@epamail.epa.gov>
Permalink:  ../398993D3.D421AAB7@epamail.epa.gov/
Also on:    postgresql.org/message-id/398993D3.D421AAB7@epamail.epa.gov

 · 

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: volpe.mark@epamail.epa.gov, Dana.Reed@clinicaldatacare.com
  Subject: Re: SQL (table transposition)
  In-Reply-To: <398993D3.D421AAB7@epamail.epa.gov>
  References: <TFSUYCTU@clinicaldatacare.com> <398993D3.D421AAB7@epamail.epa.gov>

* 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