2

I have a table which has over 60 columns. I want to reduce the number of columns and already have an idea.

So the columns look like this:

A_yes A_no B_yes B_no C_yes C_no
1     4    3     5    9     2

What I wish to get is this

Category   yes   no
A          1     4
B          3     5
C          9     2

Of course this is extremely simplified, but a solution for this would then be extended by me with the pattern I use for this. I am using PostgreSQL.

Thanks in advance!

Johnny Banana
  • 123
  • 1
  • 9

1 Answers1

1

You can use a lateral join:

select v.*
from t cross join lateral
     (values ('A', A_yes, A_no),
             ('B', B_yes, B_no),
             ('C', C_yes, C_no)
     ) v(category, yes, no);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786