Re: Dynamic table with variable number of columns - Mailing list pgsql-general
From | Thomas Burdairon |
---|---|
Subject | Re: Dynamic table with variable number of columns |
Date | |
Msg-id | ECE0BA15-EFED-406B-9B8E-4E11DE5DE85A@entelience.com Whole thread Raw |
In response to | Re: Dynamic table with variable number of columns (nkunkov@optonline.net) |
Responses |
Re: Dynamic table with variable number of columns
Re: Dynamic table with variable number of columns |
List | pgsql-general |
if i understand well you need to have an history for your products.
i would have a table B with
date products price
1/1/2006 prod1 1.0
1/1/2006 prod2 3.0
or replace prod_name py product_id, ...
Thomas
On Jul 12, 2006, at 16:08, nkunkov@optonline.net wrote:
Bruno Wolff III wrote:On Tue, Jul 11, 2006 at 06:05:18 -0700,nkunkov@optonline.net wrote:Hello,I'm a pgsql novice and here is what I'm trying to do:1. I need to create a dynamic table with the column names fetchedfrom the database using a select statement from some other table. Isit possible? Could you point me to a simple example on how to do it?2. I would like to compare the list of coulmn names which are valuesfetched from some table with the column names of the existing table.If one of the names doesn't exist as a column name of my table, I'dlike to dynamically alter the table and add a coulmn with the name justfetched from the DB.Your help is greatly appreciated.ThanksNKInformation on the column names of tables in the database are availablefrom the information schema and the catlog tables. You can find more about thisin the documentation:You might get better help by describing the actual problem you are trying tosolve rather than asking for help with a particular approach to solving thatproblem. The approach you are trying seems to be seriously broken and itwould probably be a good idea to consider other approaches.---------------------------(end of broadcast)---------------------------TIP 1: if posting/reading through Usenet, please send an appropriatesubscribe-nomail command to majordomo@postgresql.org so that yourmessage can get through to the mailing list cleanlyThank you for the suggestions.I will try to describe the problem better.I have two problems to solve. First one is that I have to transpose atable.I have table A that looks like this:date product price description1/1/2006 prod1 1.00 some product1/1/2006 prod2 3.00 other productI need to transpose this table to create table Bdate prod1 prod21/1/2006 1.00 3.00I think I can use EXECUTE statement and build the table dynamically byusing the result of the select statement for column names. Would thatbe the right approach? Are there good examples somewhere on how toimplement this?My second problem, is that after creating the above transposed table, Iwill be inserting more rows to it from table A and i might have moreproducts too. That means I will have to compare the value of productfrom table A with the column names of table B and alter the tableaccordingly. To compare coulmn names with the value of product intable A I think I can use pg_attribute function. Would that be a rightway to go?Thanks for your help.NK---------------------------(end of broadcast)---------------------------TIP 6: explain analyze is your friend
pgsql-general by date: