Reputation: 11
I have a dataset in the following format in hive table.
<br><b>|Col_1 | Col_2 |Col_3</b></<br>
<br>|abc_1 | a;b;c;d |m;n</br>
<br>|abc_2 | e;f;d |l;h;p<br>
I need to convert the data into the following format.
<br><b>|Col_1 | Col_2_OR_3 |Value</b></<br>
<br>|abc_1 | Col_2 | a</br>
<br>|abc_1 | Col_2 | b</br>
<br>|abc_1 | Col_2 | c</br>
<br>|abc_1 | Col_2 | d</br>
<br>|abc_1 | Col_3 | m</br>
<br>|abc_1 | Col_3 | n</br>
<br>|abc_2 | Col_2 | e</br>
<br>|abc_2 | Col_2 | f</br>
<br>|abc_2 | Col_2 | d</br>
<br>|abc_2 | Col_3 | l</br>
<br>|abc_2 | Col_3 | h</br>
<br>|abc_2 | Col_3 | p</br>
The data initially is unique at values in Col_1.The semicolon separated values in Col_2 and Col_3 need to be separated and transposed as a new column Value and another column needs to be created Col_2_OR_3 which has the values of column header for the column from which the semicolon separated value is picked from Col_2 or Col_3.
Upvotes: 0
Views: 1156
Reputation: 1031
select col_1,'Col_2',col_2_al
from <table_name>
lateral view explode(split(col_2, "\\\;")) col_2_al as col_2_al
union all
select col_1,'Col_3',col_2_al
from <table_name>
lateral view explode(split(col_3,"\\\;")) col_3_al as col_2_al
Upvotes: 1