I'm working on a dataset: 'data_1', to create a new column 'Category' by merging multiple columns into this single column as well as its value.
For example, data_1:
user_id | family | friend | roommate | college
===============================================
1002345 | 1 | 0 | 1 | 0
-----------------------------------------------
1002346 | 0 | 1 | 0 | 1
-----------------------------------------------
... | ... | ... | ... | ...
------------------------------------------------
I have tried 'Case When' or 'Unpivot' function in Mysql, but they are not working.
select
user_id,
category
from data_1
unpivot (category for col_name in (
data_1.family,
data_1.friend,
data_1.roommate,
data_1.college)
)
Expected Output table:
user_id | Category | Value
============================
1002345 | Family | 1
----------------------------
1002345 | Friend | 0
----------------------------
1002345 | Roommate | 1
----------------------------
1002345 | College | 0
----------------------------
1002346 | Family | 0
----------------------------
1002346 | Friend | 1
----------------------------
1002346 | Roommate | 0
----------------------------
1002346 | College | 1
----------------------------
... | ... | ...
----------------------------
Thanks!