0

I have a query that I want to transform into a new table of sorts, that will allow me to select the data along with other data. Below is the query I'm

    select t.variable,t.value from data ec
    cross join lateral json_to_recordset(calculation_data->0->'result'->'variables') 
    as t("variable" text, "value" text)

The result of the query looks like this.

variable      |   value
my_var        |    10
my_other_var  |    20

My goal here is to get a table that looks like this.

my_var   |   my_other_var
  10     |        20

I did some research into crosstab() but that doesn't seem to do what I want. Anyone know what I could use to accomplish this?

Any help will be appreciated.

Maxi Pereyra
  • 131
  • 2
  • 13
  • Does this answer your question? [Postgres - Transpose Rows to Columns](https://stackoverflow.com/questions/23060256/postgres-transpose-rows-to-columns) – Mike Organek Mar 04 '22 at 16:50
  • I previously found that post, but what I'm looking for is to make the values of the rows the new column names. That outputs the same column names. I tried that exact query but didn't get the expected resutl. – Maxi Pereyra Mar 04 '22 at 17:49
  • Once you get the right data into the table in the right format renaming the columns will be the easy bit. –  Mar 31 '22 at 11:49

0 Answers0