Re: need elegant way to store and query tables with variable headers

Lists: pgsql-general
From: "Gauthier, Dave" <dave(dot)gauthier(at)intel(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: need elegant way to store and query tables with variable headers
Date: 2013-10-08 18:27:24
Message-ID: 0AD01C53605506449BA127FB8B99E5E170D12962@FMSMSX114.amr.corp.intel.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi:

Longshot, but here goes....

Someone is asking me for a way to architect a model which will store basic table data (columns with names and rows), but the number and name of the columns are both variables. I'll call these "data-tables" here.

Example, Store this "data-table"....

'col1' 'col2' 'col3' 'val'
0 0 0 'a'
0 0 1 'b'
0 1 0 'c'

Then store this one...

'c1' 'c2' 'c3' 'c4' 'val'
0 0 0 0 'w'
0 0 0 1 'x'
0 0 1 0 'y'
0 0 1 1 'z

Order is important because they may want to reconstruct the table as is (e.g. 'w' comes before 'x' comes before 'y' ...)

Assume you can capture this info inside the context of a script (perl or something) which you would then need to store in a DB. And assume you have a "data_id" in hand for each of these "data-tables" that could be used to link them to a parent table somewhere else in the DB. Finally, assume you'll never have to query outside the context of one of these data-tables. IOW, you'll never have to come up with all "data_id" where "c3 = 1". But you may need to update 'val' where data_id=100 and c1=0 and c2=1 and, ... etc.. . And you may need to select on the data-table as if it was a real sql table.

Any ideas?

Thanks !


From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: need elegant way to store and query tables with variable headers
Date: 2013-10-08 18:47:30
Message-ID: 1381258050947-5773750.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


> Example, Store this "data-table"....
>
> 'col1' 'col2' 'col3' 'val'
> 0 0 0 'a'
> 0 0 1 'b'
> 0 1 0 'c'

Random thoughts:

If you know of a non-elegant way to accomplish this I'd recommend just using
that.

The solution to the problem will likely present itself once you are able to
accurately, and in sufficient detail, explain what it is you are trying to
accomplish.

"c(ol)1 ... c(ol)3, etc" seem like good candidates for an array typed
column. "val" is a column and you'd need some kind of "index/order" column
since relations do not have natural ordering. And then the "data_id"
foreign key column. The PK table for data_id can store what each array
position in the "data_columns" array on the FK table is named/called.

SELECT index, value FROM table_rows WHERE data_id = '...' AND cols[3] = 1
AND cols[4] = 1;

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/need-elegant-way-to-store-and-query-tables-with-variable-headers-tp5773749p5773750.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: need elegant way to store and query tables with variable headers
Date: 2013-10-08 18:52:00
Message-ID: l31k84$lkj$1@ger.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Gauthier, Dave wrote on 08.10.2013 20:27:
> Someone is asking me for a way to architect a model which will store
> basic table data (columns with names and rows), but the number and
> name of the columns are both variables. I'll call these
> "data-tables" here.
>

sounds like the hstore extension could help you here.