X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 459FA47592C for ; Tue, 1 Apr 2003 21:42:08 -0500 (EST) Received: from web12702.mail.yahoo.com (web12702.mail.yahoo.com [216.136.173.239]) by postgresql.org (Postfix) with SMTP id 2DFD54758C9 for ; Tue, 1 Apr 2003 21:42:07 -0500 (EST) Message-ID: <20030402024211.52087.qmail@web12702.mail.yahoo.com> Received: from [68.35.166.87] by web12702.mail.yahoo.com via HTTP; Tue, 01 Apr 2003 18:42:11 PST Date: Tue, 1 Apr 2003 18:42:11 -0800 (PST) From: chester c young Subject: Re: Rows as Columns To: James Taylor Cc: pgsql-sql@postgresql.org In-Reply-To: <002801c2f8ad$4888c9f0$4500a8c0@theplague> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Spam-Status: No, hits=-24.0 required=5.0 tests=BAYES_10,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, RCVD_IN_RFCI,REPLY_WITH_QUOTES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/17 X-Sequence-Number: 12718 This is obtuse, but it would work: Have a function that would dynamically build a view that has each of your type names as a column name. It could be triggered by any changes to the types table. The data table would be a left joined onto the customer table for each column. Then select from the view as you would any other view. If you'd like some suggestions on the procedure, let me know. --- James Taylor wrote: > Hi everyone, I just joined this list a few days ago... > > Now, I have a question, and I'm not even sure if this is possible > without having to make numerous statements. > > Say I have two tables with the following columns and this data: > > types: > id | name > ----------------- > 1 | first_name > 2 | last_name > 3 | phone_number > > data: > id | data | t_key | cust_id > --------------------------------- > 1 | Sam | 1 | 1 > 2 | Smith | 2 | 1 > 3 | 555-1212 | 3 | 1 > 4 | John | 1 | 2 > > > Types basically describes what the column type is. t_key references > the > type, cust_id is the user id. > > i'd want to do something like "select t.name, d.data from types t, > data > d where d.cust_id = 1" > > This would return something like: > > name | data > ------------------------ > first_name | Sam > last_name | Smith > phone_number | 555-1212 > > > Well, I need it to somehow return that data in this format: > > first_name | last_name | phone_number > ---------------------------------------- > Sam | Smith | 555-1212 > > The information in Types is not static, so I can't declare the col > names based on what you see here. > > Any suggestions on this one? __________________________________________________ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://tax.yahoo.com