根据列的值,如何在Hive中填充其他各个列

时间:2018-08-22 05:26:16

标签: sql hive

在下面的示例表中,我有一列idhobbies

   id  | hobbies   |
--------------------
0001   | reading   |
0001   | playing   |
0001   | swimming  |
0001   | badminton |

我需要基于兴趣爱好将输出作为布尔值并将其填充到单行的各个列中

 id  | reading | playing | swimming | badminton | stamp collection | 
--------------------------------------------------------------------
0001 |  1      | 1       | 1        |1          |0                 

如何转置桌子?

1 个答案:

答案 0 :(得分:0)

尝试以下情况:

select id,case when hobbies='reading' then 1 end as reading,
case when hobbies='playing' then 1 end as playing,case when hobbies='swimming' then 1 end as swimming, case when hobbies='badminton' then 1 end as 'badminton',
case when hobbies='stamp collection' then 0 end as 'stamp collection'
from tablename