在下面的示例表中,我有一列id
和hobbies
id | hobbies |
--------------------
0001 | reading |
0001 | playing |
0001 | swimming |
0001 | badminton |
我需要基于兴趣爱好将输出作为布尔值并将其填充到单行的各个列中
id | reading | playing | swimming | badminton | stamp collection |
--------------------------------------------------------------------
0001 | 1 | 1 | 1 |1 |0
如何转置桌子?
答案 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