pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: James Orr <james@lrgmail.com>
To: H Jeremy Bockholt <jeremy@wundt.psychiatry.uiowa.edu>
To: pgsql-sql@postgresql.org
Subject: Re: transposing data for a view
Date: Wed, 31 Oct 2001 14:59:02 -0500
Message-ID: <000e01c16246$7f1b1a80$1600000a@lrg.office> (raw)
References: <b773797b.0110301636.5af6bd25@posting.google.com>


----- Original Message ----- 
From: "H Jeremy Bockholt" <jeremy@wundt.psychiatry.uiowa.edu>
To: <pgsql-sql@postgresql.org>
Sent: Tuesday, October 30, 2001 7:36 PM
Subject: [SQL] transposing data for a view


> I have a generalized table:
> 
> scanid | region | volume
> -------------------------
> 1          A      34.4
> 1          B      32.1
> 1          C      29.1
> 2          A      32.4
> 2          B      33.2
> 2          C      35.6
> .
> .
> .
> 
> I want to create a flattened out view that looks like the following:
> 
> scanid | A_volume | B_volume | C_volume
> ----------------------------------------
> 1        34.4         32.1      29.1
> 2        32.4         33.2      35.6
> .
> .
> .
> 
> How do I correctly/efficiently construct a psql query to
> pivot/transpose the data?  I am using postgreSQL version 7.0.x

SELECT 
    A.scanid,
    A.volume AS A_volume,
    B.volume AS B_volume,
    C.volume AS C_volume
FROM
    table A JOIN
    table B ON (A.scanid = B.scanid) JOIN
    table C ON (B.scanid = C.scanid)
WHERE
    A.region = 'A' AND
    B.region = 'B' AND
    C.region = 'C'

- James




view thread (13+ messages)  latest in thread

Message-ID: <000e01c16246$7f1b1a80$1600000a@lrg.office>
Permalink:  ../000e01c16246$7f1b1a80$1600000a@lrg.office/
Also on:    postgresql.org/message-id/000e01c16246$7f1b1a80$1600000a@lrg.office

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: james@lrgmail.com, jeremy@wundt.psychiatry.uiowa.edu
  Subject: Re: transposing data for a view
  In-Reply-To: <000e01c16246$7f1b1a80$1600000a@lrg.office>

* 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