0

I have the table in below format :

id year column1 column2 column3
1 2000 10 20 30
1 2001 100 200 300

I want the transformed version to be like -

id Field value_2000 value_2001
1 column1 10 100
1 column2 20 200
1 column3 30 300

Appreciate any input!

I was able to take the column names in rows using unnest function. But facing some issues with taking the corresponding values.

1
  • search for unpivot Commented Mar 8, 2023 at 9:32

1 Answer 1

0

You would typically unpivot the rows to columns in a lateral join, then pivot to the target dataset with conditional aggregation.

select t.id, x.col,
    max(x.val) filter(where t.year = 2000) val_2000,
    max(x.val) filter(where t.year = 2001) val_2001
from mytable t
cross join lateral ( values
    ( 'column1', column1 ),
    ( 'column2', column2 ),
    ( 'column3', column3 )
) x(col, val)
group by t.id, x.col
order by t.id, x.col

Note that this works for a fixed list of columns and of years - otherwise you need dynamic SQL (ie generate the query text in SQL, then execute it), which is a rather different beast.

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.