pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kurt Overberg <kurt@hotdogrecords.com>
To: pgsql-sql@postgresql.org
Subject: Selecting rows as if they were columns?
Date: Thu, 09 Oct 2003 09:14:32 -0400
Message-ID: <3F855F38.9070803@hotdogrecords.com> (raw)

Gang,

I've got what I think should be a pretty simple problem- I just can't 
figure out how I'd do it.  Heck, I can't even figure out how I'd search 
for an answer to this problem.

I've got 2 tables: member and member_attr

Member is just a standard entry for a member.  Very simple:

table member {
id    integer,
name  character varying(30)
}

Member_attr is just a table of name/value pairs, such as:


table memberextra {
memberid     integer,
param1       character varying(512),
param2       character varying(512),
}

where param1 is the name, param2 is the value.

Right now, I can:

select member.id, member.name, member_attr.param1, member_attr.param2 
from member, member_attr where member.id = member_attr.memberid;

...this gives me n rows per member where n = # of attributes for that 
member, like this:

id   |  name   |   param1    |  param2
=======================================
1    |  dave   |  home       |  /home/dave
1    |  dave   |  testing    |  1
2    |  john   |  home       |  /home/john

...is there some way to make this return like this:

id  |  name    |     home   |  testing
======================================
1   |  dave    | /home/dave |  1
2   |  john    | /home/john |

...where it sort of "expands" the rows out into columns?  Am I just 
going about this the wrong way?  Thoughts on the subject would be 
greatly appreciated.

thanks!

/kurt




view thread (4+ messages)  latest in thread

Message-ID: <3F855F38.9070803@hotdogrecords.com>
Permalink:  ../3F855F38.9070803@hotdogrecords.com/
Also on:    postgresql.org/message-id/3F855F38.9070803@hotdogrecords.com

 · 

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: kurt@hotdogrecords.com
  Subject: Re: Selecting rows as if they were columns?
  In-Reply-To: <3F855F38.9070803@hotdogrecords.com>

* 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