tags:

views:

26

answers:

1

I have a table that stores records broken into fields, ie. if I have this record as follows

{
    "name": "John Doe"
    "gender": "male"
}

Then this record is stored in the table in 2 rows, as follows

[
    { "id": "1", "col": "name", "value": "John Doe" },
    { "id": "1", "col": "gender", "value": "male" }
]

If I want to write a postgresql function, that returns the record back to the original form (all attributes in a row form, instead of the one attribute one row form), how can I do it?

(the table design was done as an experiment for data warehousing purpose)

A: 

If I understand you right, you could write something like that

select a.value, b.value
from table1 a 
inner join table1 b on a.id = b.id and a.orderNum < b.orderNum 

To make it simple you could introduce field orderNum

p.s. I do not have Postgress installed, so you possibly would fix my query.