0

A key value pair(column name, value) needs to be retrieved from a table with about hundred columns.

The following query does exactly what is needed.

With dummy AS (
    Select 1 as Col1, 2 as Col2
)
SELECT
   unnest(array['Col1', 'Col2']) AS "Column Name",
   unnest(array[Col1, Col2]) AS "Value"
FROM dummy

But, it requires to specify the columns explicitly.

Is there a way to generate the same output by not specifying the column names directly at all or generate it dynamically.

1 Answer 1

1

Yes, within limits. See the crosstab function in the tablefunc extension.

It's not as nice as a real native CROSSTAB or PIVOT command, but it'll do the job.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.