Transposing rows and columns

From: Aram Fingal <fingal(at)multifactorial(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Transposing rows and columns
Date: 2010-09-16 15:42:21
Message-ID: 64AD57AD-C672-4487-BA77-ADAE1D9CDEBA@multifactorial.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm working with some people who live and breath Excel. I need to be able to move data back and forth between formats which make sense for Excel and for PostgreSQL. In some cases, this is just to accommodate what people are used to. In other cases, like statistical clustering, it's something that really has to be done.

Here is a simplified example:

I'm given data in Excel with one sheet each for a bunch of experiments. In each sheet, there are rows with different drugs at different doses and columns for each subject. The cells contain the response data. I wrote a Perl script which automates the process of extracting that data into a csv file which can be imported into a table like the following:

create table results(
expt_no int references experiments(id),
subject int references subjects(id),
drug text references drugs(name),
dose numeric,
response numeric
)

Now, suppose I do some computation on the results in the database and want to export it back out to the same kind of format that I received it (drugs and doses in rows and subjects in columns.) One method would be to use Perl. I could use DBD::Pg and loop through a bunch of queries to build a two dimensional array and then spit that back out but is there a good way to do this just in SQL? Is there a better way than creating a temporary table for each subject and then joining all the temp tables?

-Aram

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Uwe Schroeder 2010-09-16 16:04:56 Re: Transposing rows and columns
Previous Message Tom Lane 2010-09-16 15:37:34 Re: Getting FATAL: terminating connection due to administrator command